Skip to content

Commit

Permalink
Have name be generated for uniqueness (#42)
Browse files Browse the repository at this point in the history
* Add envs and reverse data source param
* Update cloud_environment.md
* update provider
* update to be environment_type
  • Loading branch information
mitch-hamm authored Apr 11, 2024
1 parent c5c7b93 commit ab93415
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 24 deletions.
8 changes: 4 additions & 4 deletions cloud/data_source_cloud_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
1 change: 1 addition & 0 deletions cloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
33 changes: 18 additions & 15 deletions cloud/resource_cloud_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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") ||
Expand All @@ -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)
Expand All @@ -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,
Expand Down Expand Up @@ -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{})
Expand All @@ -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,
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
}
Expand All @@ -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))
Expand All @@ -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
}
7 changes: 3 additions & 4 deletions cloud/validate_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion docs/resources/cloud_environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit ab93415

Please sign in to comment.