-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge namespaces into feature/vspherecluster
- Loading branch information
Showing
8 changed files
with
465 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.