diff --git a/cloud/data_source_cloud_environment.go b/cloud/data_source_cloud_environment.go index 05b62e5..22fae75 100644 --- a/cloud/data_source_cloud_environment.go +++ b/cloud/data_source_cloud_environment.go @@ -40,16 +40,16 @@ func dataSourceCloudEnvironment() *schema.Resource { }, }, Schema: map[string]*schema.Schema{ - "organization": { + "name": { Type: schema.TypeString, Required: true, - Description: descriptions["organization"], + Description: descriptions["cloud_environment_name"], ValidateFunc: validateNotBlank, }, - "name": { + "organization": { Type: schema.TypeString, Required: true, - Description: descriptions["cloud_environment_name"], + Description: descriptions["organization"], ValidateFunc: validateNotBlank, }, "region": { diff --git a/cloud/provider.go b/cloud/provider.go index af32ea4..b01cc7e 100644 --- a/cloud/provider.go +++ b/cloud/provider.go @@ -91,6 +91,7 @@ func init() { "gcp": "GCP configuration for the connection", "azure": "Azure configuration for the connection", "cloud_connection_name": "Name of the cloud connection", + "environment_type": "Type of the cloud environment, either: dev, test, staging, production, acc, qa or poc", "cloud_environment_name": "Name of the cloud environment", "apikey_name": "The name of the api key", "apikey_description": "The description of the api key", diff --git a/cloud/resource_cloud_environment.go b/cloud/resource_cloud_environment.go index f004e18..f7a3e31 100644 --- a/cloud/resource_cloud_environment.go +++ b/cloud/resource_cloud_environment.go @@ -35,13 +35,11 @@ func resourceCloudEnvironment() *schema.Resource { DeleteContext: resourceCloudEnvironmentDelete, CustomizeDiff: func(ctx context.Context, diff *schema.ResourceDiff, i interface{}) error { oldOrg, _ := diff.GetChange("organization") - oldName, _ := diff.GetChange("name") - if oldOrg.(string) == "" && oldName.(string) == "" { + if oldOrg.(string) == "" { // This is create event, so we don't need to check the diff. return nil } - if diff.HasChange("name") || - diff.HasChanges("organization") || + if diff.HasChanges("organization") || diff.HasChanges("cloud_connection_name") || diff.HasChanges("region") || diff.HasChanges("network_id") || @@ -55,7 +53,6 @@ func resourceCloudEnvironment() *schema.Resource { StateContext: func(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { organizationInstance := strings.Split(d.Id(), "/") _ = d.Set("organization", organizationInstance[0]) - _ = d.Set("name", organizationInstance[1]) err := resourceCloudEnvironmentRead(ctx, d, meta) if err.HasError() { return nil, fmt.Errorf("import %q: %s", d.Id(), err[0].Summary) @@ -70,11 +67,11 @@ func resourceCloudEnvironment() *schema.Resource { Description: descriptions["organization"], ValidateFunc: validateNotBlank, }, - "name": { + "environment_type": { Type: schema.TypeString, Required: true, - Description: descriptions["cloud_environment_name"], - ValidateFunc: validateCloudEnvionmentName, + Description: descriptions["environment_type"], + ValidateFunc: validateCloudEnvionmentType, }, "region": { Type: schema.TypeString, @@ -112,7 +109,7 @@ func resourceCloudEnvironment() *schema.Resource { func resourceCloudEnvironmentCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { namespace := d.Get("organization").(string) - name := d.Get("name").(string) + cloudEnvironmentType := d.Get("environment_type").(string) region := d.Get("region").(string) cloudConnectionName := d.Get("cloud_connection_name").(string) network := d.Get("network").([]interface{}) @@ -127,8 +124,10 @@ func resourceCloudEnvironmentCreate(ctx context.Context, d *schema.ResourceData, APIVersion: cloudv1alpha1.SchemeGroupVersion.String(), }, ObjectMeta: metav1.ObjectMeta{ - Name: name, Namespace: namespace, + Annotations: map[string]string{ + "cloud.streamnative.io/environment-type": cloudEnvironmentType, + }, }, Spec: cloudv1alpha1.CloudEnvironmentSpec{ CloudConnectionName: cloudConnectionName, @@ -161,6 +160,9 @@ func resourceCloudEnvironmentCreate(ctx context.Context, d *schema.ResourceData, if err != nil { return diag.FromErr(fmt.Errorf("ERROR_CREATE_CLOUD_ENVIRONMENT: %w", err)) } + + d.SetId(fmt.Sprintf("%s/%s", ce.ObjectMeta.Namespace, ce.ObjectMeta.Name)) + if ce.Status.Conditions != nil { ready := false for _, condition := range ce.Status.Conditions { @@ -170,7 +172,6 @@ func resourceCloudEnvironmentCreate(ctx context.Context, d *schema.ResourceData, } if ready { _ = d.Set("organization", namespace) - _ = d.Set("name", name) return resourceCloudEnvironmentRead(ctx, d, meta) } } @@ -189,7 +190,8 @@ func resourceCloudEnvironmentCreate(ctx context.Context, d *schema.ResourceData, func resourceCloudEnvironmentRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { namespace := d.Get("organization").(string) - name := d.Get("name").(string) + name := strings.Split(d.Id(), "/")[1] + clientSet, err := getClientSet(getFactoryFromMeta(meta)) if err != nil { return diag.FromErr(fmt.Errorf("ERROR_INIT_CLIENT_ON_READ_SERVICE_ACCOUNT: %w", err)) @@ -216,15 +218,16 @@ func resourceCloudEnvironmentUpdate(ctx context.Context, d *schema.ResourceData, func resourceCloudEnvironmentDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { clientSet, err := getClientSet(getFactoryFromMeta(meta)) + name := strings.Split(d.Id(), "/")[1] + namespace := d.Get("organization").(string) + if err != nil { return diag.FromErr(fmt.Errorf("ERROR_INIT_CLIENT_ON_DELETE_CLOUD_ENVIRONMENT: %w", err)) } - namespace := d.Get("organization").(string) - name := d.Get("name").(string) + err = clientSet.CloudV1alpha1().CloudEnvironments(namespace).Delete(ctx, name, metav1.DeleteOptions{}) if err != nil { return diag.FromErr(fmt.Errorf("DELETE_CLOUD_ENVIRONMENT: %w", err)) } - _ = d.Set("name", "") return nil } diff --git a/cloud/validate_helpers.go b/cloud/validate_helpers.go index a998ae3..bebe05d 100644 --- a/cloud/validate_helpers.go +++ b/cloud/validate_helpers.go @@ -64,12 +64,11 @@ func validateAuditLog(val interface{}, key string) (warns []string, errs []error return } -func validateCloudEnvionmentName(val interface{}, key string) (warns []string, errs []error) { - maxNameLength := 28 +func validateCloudEnvionmentType(val interface{}, key string) (warns []string, errs []error) { v := val.(string) - if len(v) > maxNameLength || len(v) < 1 { + if v != "dev" && v != "test" && v != "staging" && v != "production" && v != "acc" && v != "qa" && v != "poc" { errs = append(errs, fmt.Errorf( - "%q should be shorter than or equal to %d and greater than 0, got: %s", key, maxNameLength, v)) + "%q should be one of: dev, test, staging, production, acc, qa or poc, got: %s", key, v)) } return } diff --git a/docs/resources/cloud_environment.md b/docs/resources/cloud_environment.md index 333c36e..46d0ca7 100644 --- a/docs/resources/cloud_environment.md +++ b/docs/resources/cloud_environment.md @@ -18,7 +18,7 @@ description: |- ### Required - `cloud_connection_name` (String) Name of the cloud connection -- `name` (String) Name of the cloud environment +- `environment_type` (String) Type of the cloud environment, either: dev, test, staging, production, acc, qa or poc - `network` (Block List, Min: 1) (see [below for nested schema](#nestedblock--network)) - `organization` (String) The organization name - `region` (String)