Skip to content

Commit

Permalink
Merge namespaces into feature/vspherecluster
Browse files Browse the repository at this point in the history
  • Loading branch information
guruprasad-afk committed Jun 7, 2022
2 parents f23e64b + dc02b8c commit 4fe17a7
Show file tree
Hide file tree
Showing 8 changed files with 465 additions and 7 deletions.
10 changes: 5 additions & 5 deletions docs/data-sources/aws_nodepool.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
---
page_title: "TMC: tmc_nodepool"
page_title: "TMC: tmc_aws_nodepool"
layout: "tmc"
subcategory: "TKG Cluster"
description: |-
Get information on a specific nodepool of a AWS cluster in Tanzu Mission Control (TMC)
---

# Data Source: tmc_cluster
# Data Source: tmc_aws_nodepool

The TMC Nodepool data resource can be used to get the information of a nodepool for a AWS cluster in Tanzu Mission Control (TMC).

Expand All @@ -31,9 +31,9 @@ The following arguments are supported:
## Attributes Reference

* `id` - The UID of the Tanzu Cluster.
* `description` - (Optional) The description of the nodepool.
* `node_labels` - (Optional) A map of node labels to assign to the resource.
* `cloud_labels` - (Optional) A map of cloud labels to assign to the resource.
* `description` - The description of the nodepool.
* `node_labels` - A map of node labels to assign to the resource.
* `cloud_labels` - A map of cloud labels to assign to the resource.
* `availability_zone` - The AWS availability zone for the cluster's worker nodes.
* `instance_type` - Instance type of the EC2 nodes to be used as part of the nodepool.
* `version` - Version of Kubernetes to be used in the cluster.
36 changes: 36 additions & 0 deletions docs/data-sources/namespace.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
page_title: "TMC: tmc_namespace"
layout: "tmc"
subcategory: "Tanzu Namespace"
description: |-
Get information on a specific namespace of a cluster in Tanzu Mission Control (TMC)
---

# Data Source: tmc_namespace

The TMC Namespace data resource can be used to get the information of a namespace for a cluster in Tanzu Mission Control (TMC).

```terraform
data "tmc_namespace" "example" {
cluster_name = "example-cluster"
management_cluster = "example-hosted"
provisioner_name = "example-provisioner"
name = "example-ns"
}
```

## Argument Reference

The following arguments are supported:

* `name` - (Required) The name of the namespace. Changing the name forces recreation of this resource.
* `cluster_name` - (Required) The name of the Tanzu Cluster for which the namespace is to be created.
* `management_cluster` - (Required) Name of the management cluster used to provision the cluster.
* `provisioner_name` - (Required) Name of the provisioner to be used.

## Attributes Reference

* `id` - The UID of the Tanzu Cluster.
* `description` - The description of the nodepool.
* `workspace_name` - Name of the workspace for the created namespace.
* `labels` - A map of labels to assign to the resource.
4 changes: 2 additions & 2 deletions docs/resources/aws_nodepool.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
---
page_title: "TMC: tmc_nodepool"
page_title: "TMC: tmc_aws_nodepool"
layout: "tmc"
subcategory: "TKG Cluster"
description: |-
Creates and manages a nodepool for a AWS cluster in the TMC platform
---

# Resource: tmc_cluster
# Resource: tmc_aws_nodepool

The TMC Cluster resource allows requesting the creation of a nodepool for a AWS cluster in Tanzu Mission Control (TMC).

Expand Down
43 changes: 43 additions & 0 deletions docs/resources/namespace.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
page_title: "TMC: tmc_namespace"
layout: "tmc"
subcategory: "Tanzu Namespace"
description: |-
Creates and manages a namespace for a cluster in the TMC platform
---

# Resource: tmc_namespace

The TMC Namespace resource allows requesting the creation of a namespace for a cluster in Tanzu Mission Control (TMC).

```terraform
resource "tmc_namespace" "example" {
name = "example-ns"
description = "terraform created mgmt cluster"
cluster_name = "example-cluster"
management_cluster = "example-hosted"
provisioner_name = "example-provisioner"
workspace_name = "default"
labels = {
"CreatedBy" = "terraform"
}
}
```

## Argument Reference

The following arguments are supported:

* `name` - (Required) The name of the Namespace. Changing the name forces recreation of this resource.
* `description` - (Optional) The description of the namespace.
* `cluster_name` - (Required) The name of the Tanzu Cluster for which the namespace is to be created.
* `management_cluster` - (Required) Name of the management cluster used to provision the cluster.
* `provisioner_name` - (Required) Name of the provisioner to be used.
* `workspace_name` - (Required) Name of the workspace for the created namespace.
* `labels` - (Optional) A map of labels to assign to the resource.

## Attributes Reference

In addition to all arguments above, the following attribute is exported:

* `id` - The UID of the Tanzu Namespace.
107 changes: 107 additions & 0 deletions tanzuclient/namespace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package tanzuclient

import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)

type NamespaceSpec struct {
WorkspaceName string `json:"workspaceName"`
}

type Namespace struct {
FullName *FullName `json:"fullName"`
Meta *MetaData `json:"meta"`
Spec *NamespaceSpec `json:"spec"`
}

type NamespaceJsonObject struct {
Namespace Namespace `json:"namespace"`
}

type NamespaceOpts struct {
Description string
Labels map[string]interface{}
ManagementCluster string
ProvisionerName string
ClusterName string
WorkspaceName string
}

func (c *Client) CreateNamespace(name string, opts NamespaceOpts) (*Namespace, error) {
requestURL := fmt.Sprintf("%s/v1alpha1/clusters/%s/namespaces?fullName.managementClusterName=%s&fullName.provisionerName=%s", c.baseURL, opts.ClusterName, opts.ManagementCluster, opts.ProvisionerName)

newNamespace := &Namespace{
FullName: &FullName{
Name: name,
ProvisionerName: opts.ProvisionerName,
ManagementClusterName: opts.ManagementCluster,
ClusterName: opts.ClusterName,
},
Meta: &MetaData{
Description: opts.Description,
Labels: opts.Labels,
},
Spec: &NamespaceSpec{
WorkspaceName: opts.WorkspaceName,
},
}

newNamespaceObject := &NamespaceJsonObject{
Namespace: *newNamespace,
}

json_data, err := json.Marshal(newNamespaceObject) // returns []byte
if err != nil {
return nil, err
}

req, err := http.NewRequest("POST", requestURL, bytes.NewBuffer(json_data))
if err != nil {
return nil, err
}

res := NamespaceJsonObject{}

if err := c.sendRequest(req, &res); err != nil {
return nil, err
}

return &res.Namespace, nil
}

func (c *Client) GetNamespace(name string, clusterName string, managementClusterName string, provisionerName string) (*Namespace, error) {
requestURL := fmt.Sprintf("%s/v1alpha1/clusters/%s/namespaces/%s?fullName.managementClusterName=%s&fullName.provisionerName=%s", c.baseURL, clusterName, name, managementClusterName, provisionerName)

req, err := http.NewRequest("GET", requestURL, nil)
if err != nil {
return nil, err
}

res := NamespaceJsonObject{}

if err := c.sendRequest(req, &res); err != nil {
return nil, err
}

return &res.Namespace, nil
}

func (c *Client) DeleteNamespace(name string, clusterName string, managementClusterName string, provisionerName string) error {
requestURL := fmt.Sprintf("%s/v1alpha1/clusters/%s/namespaces/%s?fullName.managementClusterName=%s&fullName.provisionerName=%s", c.baseURL, clusterName, name, managementClusterName, provisionerName)

req, err := http.NewRequest("DELETE", requestURL, nil)
if err != nil {
return err
}

res := NamespaceJsonObject{}

if err := c.sendRequest(req, &res); err != nil {
return err
}

return nil
}
98 changes: 98 additions & 0 deletions tmc/data_source_tmc_namespace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package tmc

import (
"context"
"fmt"

"github.com/codaglobal/terraform-provider-tmc/tanzuclient"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceTmcNamespace() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceTmcNamespaceRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: "Unique ID of the Cluster Group",
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "Unique Name of the Namespace in your Org",
ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) {
v := val.(string)
if !IsValidTanzuName(v) {
errs = append(errs, fmt.Errorf("name should contain only lowercase letters, numbers or hyphens and should begin with either an alphabet or number"))
}
return
},
},
"cluster_name": {
Type: schema.TypeString,
Required: true,
Description: "Name of the cluster in which the namespace is to be created",
},
"description": {
Type: schema.TypeString,
Computed: true,
Description: "Description of the Namespace",
},
"management_cluster": {
Type: schema.TypeString,
Required: true,
Description: "Name of the management cluster used",
},
"provisioner_name": {
Type: schema.TypeString,
Required: true,
Description: "Name of the provisioner",
},
"labels": labelsSchemaComputed(),
"workspace_name": {
Type: schema.TypeString,
Computed: true,
Description: "Name of the workspace for the namespace",
},
},
}
}

func dataSourceTmcNamespaceRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics

client := m.(*tanzuclient.Client)

NamespaceName := d.Get("name").(string)
clusterName := d.Get("cluster_name").(string)
managementClusterName := d.Get("management_cluster").(string)
provisionerName := d.Get("provisioner_name").(string)

Namespace, err := client.GetNamespace(NamespaceName, clusterName, managementClusterName, provisionerName)
if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Failed to read namespace",
Detail: fmt.Sprintf("Error reading resource %s: %s", d.Get("name"), err),
})
return diags
}

d.Set("description", Namespace.Meta.Description)
if err := d.Set("labels", Namespace.Meta.Labels); err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Failed to read namespace",
Detail: fmt.Sprintf("Error setting labels for resource %s: %s", d.Get("name"), err),
})
return diags
}

d.Set("workspace_name", Namespace.Spec.WorkspaceName)

d.SetId(Namespace.Meta.UID)

return nil
}
2 changes: 2 additions & 0 deletions tmc/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func Provider() *schema.Provider {
"tmc_aws_storage_credential": dataSourceTmcAwsStorageCredential(),
"tmc_observability_credential": dataSourceTmcObservabilityCredential(),
"tmc_cluster_backup": dataSourceTmcClusterBackup(),
"tmc_namespace": dataSourceTmcNamespace(),
},

// List of Resources supported by the provider
Expand All @@ -57,6 +58,7 @@ func Provider() *schema.Provider {
"tmc_observability_credential": resourceTmcObservabilityCredential(),
"tmc_cluster_backup": resourceTmcClusterBackup(),
"tmc_vsphere_cluster": resourceVsphereCluster(),
"tmc_namespace": resourceTmcNamespace(),
},
}

Expand Down
Loading

0 comments on commit 4fe17a7

Please sign in to comment.