| // Copyright (c) HashiCorp, Inc. |
| // SPDX-License-Identifier: MPL-2.0 |
| |
| // ---------------------------------------------------------------------------- |
| // |
| // *** AUTO GENERATED CODE *** Type: DCL *** |
| // |
| // ---------------------------------------------------------------------------- |
| // |
| // This file is managed by Magic Modules (https://github.com/GoogleCloudPlatform/magic-modules) |
| // and is based on the DCL (https://github.com/GoogleCloudPlatform/declarative-resource-client-library). |
| // Changes will need to be made to the DCL or Magic Modules instead of here. |
| // |
| // We are not currently able to accept contributions to this file. If changes |
| // are required, please file an issue at https://github.com/hashicorp/terraform-provider-google/issues/new/choose |
| // |
| // ---------------------------------------------------------------------------- |
| |
| package containerazure |
| |
| import ( |
| "context" |
| "fmt" |
| "log" |
| "time" |
| |
| "github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff" |
| "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| |
| dcl "github.com/GoogleCloudPlatform/declarative-resource-client-library/dcl" |
| containerazure "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/containerazure/beta" |
| |
| "github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgdclresource" |
| "github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource" |
| transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport" |
| ) |
| |
| func ResourceContainerAzureClient() *schema.Resource { |
| return &schema.Resource{ |
| Create: resourceContainerAzureClientCreate, |
| Read: resourceContainerAzureClientRead, |
| Delete: resourceContainerAzureClientDelete, |
| |
| Importer: &schema.ResourceImporter{ |
| State: resourceContainerAzureClientImport, |
| }, |
| |
| Timeouts: &schema.ResourceTimeout{ |
| Create: schema.DefaultTimeout(20 * time.Minute), |
| Delete: schema.DefaultTimeout(20 * time.Minute), |
| }, |
| CustomizeDiff: customdiff.All( |
| tpgresource.DefaultProviderProject, |
| ), |
| |
| Schema: map[string]*schema.Schema{ |
| "application_id": { |
| Type: schema.TypeString, |
| Required: true, |
| ForceNew: true, |
| Description: "The Azure Active Directory Application ID.", |
| }, |
| |
| "location": { |
| Type: schema.TypeString, |
| Required: true, |
| ForceNew: true, |
| Description: "The location for the resource", |
| }, |
| |
| "name": { |
| Type: schema.TypeString, |
| Required: true, |
| ForceNew: true, |
| Description: "The name of this resource.", |
| }, |
| |
| "tenant_id": { |
| Type: schema.TypeString, |
| Required: true, |
| ForceNew: true, |
| Description: "The Azure Active Directory Tenant ID.", |
| }, |
| |
| "project": { |
| Type: schema.TypeString, |
| Computed: true, |
| Optional: true, |
| ForceNew: true, |
| DiffSuppressFunc: tpgresource.CompareSelfLinkOrResourceName, |
| Description: "The project for the resource", |
| }, |
| |
| "certificate": { |
| Type: schema.TypeString, |
| Computed: true, |
| Description: "Output only. The PEM encoded x509 certificate.", |
| }, |
| |
| "create_time": { |
| Type: schema.TypeString, |
| Computed: true, |
| Description: "Output only. The time at which this resource was created.", |
| }, |
| |
| "uid": { |
| Type: schema.TypeString, |
| Computed: true, |
| Description: "Output only. A globally unique identifier for the client.", |
| }, |
| }, |
| } |
| } |
| |
| func resourceContainerAzureClientCreate(d *schema.ResourceData, meta interface{}) error { |
| config := meta.(*transport_tpg.Config) |
| project, err := tpgresource.GetProject(d, config) |
| if err != nil { |
| return err |
| } |
| |
| obj := &containerazure.AzureClient{ |
| ApplicationId: dcl.String(d.Get("application_id").(string)), |
| Location: dcl.String(d.Get("location").(string)), |
| Name: dcl.String(d.Get("name").(string)), |
| TenantId: dcl.String(d.Get("tenant_id").(string)), |
| Project: dcl.String(project), |
| } |
| |
| id, err := obj.ID() |
| if err != nil { |
| return fmt.Errorf("error constructing id: %s", err) |
| } |
| d.SetId(id) |
| directive := tpgdclresource.CreateDirective |
| userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) |
| if err != nil { |
| return err |
| } |
| billingProject := project |
| // err == nil indicates that the billing_project value was found |
| if bp, err := tpgresource.GetBillingProject(d, config); err == nil { |
| billingProject = bp |
| } |
| client := transport_tpg.NewDCLContainerAzureClient(config, userAgent, billingProject, d.Timeout(schema.TimeoutCreate)) |
| if bp, err := tpgresource.ReplaceVars(d, config, client.Config.BasePath); err != nil { |
| d.SetId("") |
| return fmt.Errorf("Could not format %q: %w", client.Config.BasePath, err) |
| } else { |
| client.Config.BasePath = bp |
| } |
| res, err := client.ApplyClient(context.Background(), obj, directive...) |
| |
| if _, ok := err.(dcl.DiffAfterApplyError); ok { |
| log.Printf("[DEBUG] Diff after apply returned from the DCL: %s", err) |
| } else if err != nil { |
| // The resource didn't actually create |
| d.SetId("") |
| return fmt.Errorf("Error creating Client: %s", err) |
| } |
| |
| log.Printf("[DEBUG] Finished creating Client %q: %#v", d.Id(), res) |
| |
| return resourceContainerAzureClientRead(d, meta) |
| } |
| |
| func resourceContainerAzureClientRead(d *schema.ResourceData, meta interface{}) error { |
| config := meta.(*transport_tpg.Config) |
| project, err := tpgresource.GetProject(d, config) |
| if err != nil { |
| return err |
| } |
| |
| obj := &containerazure.AzureClient{ |
| ApplicationId: dcl.String(d.Get("application_id").(string)), |
| Location: dcl.String(d.Get("location").(string)), |
| Name: dcl.String(d.Get("name").(string)), |
| TenantId: dcl.String(d.Get("tenant_id").(string)), |
| Project: dcl.String(project), |
| } |
| |
| userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) |
| if err != nil { |
| return err |
| } |
| billingProject := project |
| // err == nil indicates that the billing_project value was found |
| if bp, err := tpgresource.GetBillingProject(d, config); err == nil { |
| billingProject = bp |
| } |
| client := transport_tpg.NewDCLContainerAzureClient(config, userAgent, billingProject, d.Timeout(schema.TimeoutRead)) |
| if bp, err := tpgresource.ReplaceVars(d, config, client.Config.BasePath); err != nil { |
| d.SetId("") |
| return fmt.Errorf("Could not format %q: %w", client.Config.BasePath, err) |
| } else { |
| client.Config.BasePath = bp |
| } |
| res, err := client.GetClient(context.Background(), obj) |
| if err != nil { |
| resourceName := fmt.Sprintf("ContainerAzureClient %q", d.Id()) |
| return tpgdclresource.HandleNotFoundDCLError(err, d, resourceName) |
| } |
| |
| if err = d.Set("application_id", res.ApplicationId); err != nil { |
| return fmt.Errorf("error setting application_id in state: %s", err) |
| } |
| if err = d.Set("location", res.Location); err != nil { |
| return fmt.Errorf("error setting location in state: %s", err) |
| } |
| if err = d.Set("name", res.Name); err != nil { |
| return fmt.Errorf("error setting name in state: %s", err) |
| } |
| if err = d.Set("tenant_id", res.TenantId); err != nil { |
| return fmt.Errorf("error setting tenant_id in state: %s", err) |
| } |
| if err = d.Set("project", res.Project); err != nil { |
| return fmt.Errorf("error setting project in state: %s", err) |
| } |
| if err = d.Set("certificate", res.Certificate); err != nil { |
| return fmt.Errorf("error setting certificate in state: %s", err) |
| } |
| if err = d.Set("create_time", res.CreateTime); err != nil { |
| return fmt.Errorf("error setting create_time in state: %s", err) |
| } |
| if err = d.Set("uid", res.Uid); err != nil { |
| return fmt.Errorf("error setting uid in state: %s", err) |
| } |
| |
| return nil |
| } |
| |
| func resourceContainerAzureClientDelete(d *schema.ResourceData, meta interface{}) error { |
| config := meta.(*transport_tpg.Config) |
| project, err := tpgresource.GetProject(d, config) |
| if err != nil { |
| return err |
| } |
| |
| obj := &containerazure.AzureClient{ |
| ApplicationId: dcl.String(d.Get("application_id").(string)), |
| Location: dcl.String(d.Get("location").(string)), |
| Name: dcl.String(d.Get("name").(string)), |
| TenantId: dcl.String(d.Get("tenant_id").(string)), |
| Project: dcl.String(project), |
| } |
| |
| log.Printf("[DEBUG] Deleting Client %q", d.Id()) |
| userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) |
| if err != nil { |
| return err |
| } |
| billingProject := project |
| // err == nil indicates that the billing_project value was found |
| if bp, err := tpgresource.GetBillingProject(d, config); err == nil { |
| billingProject = bp |
| } |
| client := transport_tpg.NewDCLContainerAzureClient(config, userAgent, billingProject, d.Timeout(schema.TimeoutDelete)) |
| if bp, err := tpgresource.ReplaceVars(d, config, client.Config.BasePath); err != nil { |
| d.SetId("") |
| return fmt.Errorf("Could not format %q: %w", client.Config.BasePath, err) |
| } else { |
| client.Config.BasePath = bp |
| } |
| if err := client.DeleteClient(context.Background(), obj); err != nil { |
| return fmt.Errorf("Error deleting Client: %s", err) |
| } |
| |
| log.Printf("[DEBUG] Finished deleting Client %q", d.Id()) |
| return nil |
| } |
| |
| func resourceContainerAzureClientImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { |
| config := meta.(*transport_tpg.Config) |
| |
| if err := tpgresource.ParseImportId([]string{ |
| "projects/(?P<project>[^/]+)/locations/(?P<location>[^/]+)/azureClients/(?P<name>[^/]+)", |
| "(?P<project>[^/]+)/(?P<location>[^/]+)/(?P<name>[^/]+)", |
| "(?P<location>[^/]+)/(?P<name>[^/]+)", |
| }, d, config); err != nil { |
| return nil, err |
| } |
| |
| // Replace import id for the resource id |
| id, err := tpgresource.ReplaceVarsForId(d, config, "projects/{{project}}/locations/{{location}}/azureClients/{{name}}") |
| if err != nil { |
| return nil, fmt.Errorf("Error constructing id: %s", err) |
| } |
| d.SetId(id) |
| |
| return []*schema.ResourceData{d}, nil |
| } |