Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Have name be generated for uniqueness #42

Merged
merged 11 commits into from
Apr 11, 2024
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,
mitch-hamm marked this conversation as resolved.
Show resolved Hide resolved
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
2 changes: 1 addition & 1 deletion cloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func init() {
"gcp": "GCP configuration for the connection",
"azure": "Azure configuration for the connection",
"cloud_connection_name": "Name of the cloud connection",
"cloud_environment_name": "Name of the cloud environment",
"cloud_environment_type": "Type of the cloud environment, either: test, staging or production",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"cloud_environment_type": "Type of the cloud environment, either: test, staging or production",
"cloud_environment_type": "Type of the cloud environment, either: dev, test, staging, production, acc, qa or poc",

"apikey_name": "The name of the api key",
"apikey_description": "The description of the api key",
"revoke": "Whether to revoke the api key, if set to true, the api key will be revoked." +
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": {
"type": {
Type: schema.TypeString,
Required: true,
Description: descriptions["cloud_environment_name"],
ValidateFunc: validateCloudEnvionmentName,
Description: descriptions["cloud_environment_type"],
ValidateFunc: validateCloudEnvionmentType,
mitch-hamm marked this conversation as resolved.
Show resolved Hide resolved
},
"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("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 != "test" && v != "staging" && v != "production" && v != "acc" && v != "qa" && v != "poc" {
mitch-hamm marked this conversation as resolved.
Show resolved Hide resolved
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: test, staging, production, acc, qa or poc, got: %s", key, v))
mitch-hamm marked this conversation as resolved.
Show resolved Hide resolved
}
return
}
Expand Down
Loading