| // 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 eventarc |
| |
| 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" |
| eventarc "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/eventarc/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 ResourceEventarcChannel() *schema.Resource { |
| return &schema.Resource{ |
| Create: resourceEventarcChannelCreate, |
| Read: resourceEventarcChannelRead, |
| Update: resourceEventarcChannelUpdate, |
| Delete: resourceEventarcChannelDelete, |
| |
| Importer: &schema.ResourceImporter{ |
| State: resourceEventarcChannelImport, |
| }, |
| |
| Timeouts: &schema.ResourceTimeout{ |
| Create: schema.DefaultTimeout(20 * time.Minute), |
| Update: schema.DefaultTimeout(20 * time.Minute), |
| Delete: schema.DefaultTimeout(20 * time.Minute), |
| }, |
| CustomizeDiff: customdiff.All( |
| tpgresource.DefaultProviderProject, |
| ), |
| |
| Schema: map[string]*schema.Schema{ |
| "location": { |
| Type: schema.TypeString, |
| Required: true, |
| ForceNew: true, |
| Description: "The location for the resource", |
| }, |
| |
| "name": { |
| Type: schema.TypeString, |
| Required: true, |
| ForceNew: true, |
| Description: "Required. The resource name of the channel. Must be unique within the location on the project.", |
| }, |
| |
| "crypto_key_name": { |
| Type: schema.TypeString, |
| Optional: true, |
| DiffSuppressFunc: tpgresource.CompareSelfLinkOrResourceName, |
| Description: "Optional. Resource name of a KMS crypto key (managed by the user) used to encrypt/decrypt their event data. It must match the pattern `projects/*/locations/*/keyRings/*/cryptoKeys/*`.", |
| }, |
| |
| "project": { |
| Type: schema.TypeString, |
| Computed: true, |
| Optional: true, |
| ForceNew: true, |
| DiffSuppressFunc: tpgresource.CompareSelfLinkOrResourceName, |
| Description: "The project for the resource", |
| }, |
| |
| "third_party_provider": { |
| Type: schema.TypeString, |
| Optional: true, |
| ForceNew: true, |
| Description: "The name of the event provider (e.g. Eventarc SaaS partner) associated with the channel. This provider will be granted permissions to publish events to the channel. Format: `projects/{project}/locations/{location}/providers/{provider_id}`.", |
| }, |
| |
| "activation_token": { |
| Type: schema.TypeString, |
| Computed: true, |
| Description: "Output only. The activation token for the channel. The token must be used by the provider to register the channel for publishing.", |
| }, |
| |
| "create_time": { |
| Type: schema.TypeString, |
| Computed: true, |
| Description: "Output only. The creation time.", |
| }, |
| |
| "pubsub_topic": { |
| Type: schema.TypeString, |
| Computed: true, |
| Description: "Output only. The name of the Pub/Sub topic created and managed by Eventarc system as a transport for the event delivery. Format: `projects/{project}/topics/{topic_id}`.", |
| }, |
| |
| "state": { |
| Type: schema.TypeString, |
| Computed: true, |
| Description: "Output only. The state of a Channel. Possible values: STATE_UNSPECIFIED, PENDING, ACTIVE, INACTIVE", |
| }, |
| |
| "uid": { |
| Type: schema.TypeString, |
| Computed: true, |
| Description: "Output only. Server assigned unique identifier for the channel. The value is a UUID4 string and guaranteed to remain unchanged until the resource is deleted.", |
| }, |
| |
| "update_time": { |
| Type: schema.TypeString, |
| Computed: true, |
| Description: "Output only. The last-modified time.", |
| }, |
| }, |
| } |
| } |
| |
| func resourceEventarcChannelCreate(d *schema.ResourceData, meta interface{}) error { |
| config := meta.(*transport_tpg.Config) |
| project, err := tpgresource.GetProject(d, config) |
| if err != nil { |
| return err |
| } |
| |
| obj := &eventarc.Channel{ |
| Location: dcl.String(d.Get("location").(string)), |
| Name: dcl.String(d.Get("name").(string)), |
| CryptoKeyName: dcl.String(d.Get("crypto_key_name").(string)), |
| Project: dcl.String(project), |
| ThirdPartyProvider: dcl.String(d.Get("third_party_provider").(string)), |
| } |
| |
| 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.NewDCLEventarcClient(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.ApplyChannel(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 Channel: %s", err) |
| } |
| |
| log.Printf("[DEBUG] Finished creating Channel %q: %#v", d.Id(), res) |
| |
| return resourceEventarcChannelRead(d, meta) |
| } |
| |
| func resourceEventarcChannelRead(d *schema.ResourceData, meta interface{}) error { |
| config := meta.(*transport_tpg.Config) |
| project, err := tpgresource.GetProject(d, config) |
| if err != nil { |
| return err |
| } |
| |
| obj := &eventarc.Channel{ |
| Location: dcl.String(d.Get("location").(string)), |
| Name: dcl.String(d.Get("name").(string)), |
| CryptoKeyName: dcl.String(d.Get("crypto_key_name").(string)), |
| Project: dcl.String(project), |
| ThirdPartyProvider: dcl.String(d.Get("third_party_provider").(string)), |
| } |
| |
| 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.NewDCLEventarcClient(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.GetChannel(context.Background(), obj) |
| if err != nil { |
| resourceName := fmt.Sprintf("EventarcChannel %q", d.Id()) |
| return tpgdclresource.HandleNotFoundDCLError(err, d, resourceName) |
| } |
| |
| 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("crypto_key_name", res.CryptoKeyName); err != nil { |
| return fmt.Errorf("error setting crypto_key_name 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("third_party_provider", res.ThirdPartyProvider); err != nil { |
| return fmt.Errorf("error setting third_party_provider in state: %s", err) |
| } |
| if err = d.Set("activation_token", res.ActivationToken); err != nil { |
| return fmt.Errorf("error setting activation_token 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("pubsub_topic", res.PubsubTopic); err != nil { |
| return fmt.Errorf("error setting pubsub_topic in state: %s", err) |
| } |
| if err = d.Set("state", res.State); err != nil { |
| return fmt.Errorf("error setting state in state: %s", err) |
| } |
| if err = d.Set("uid", res.Uid); err != nil { |
| return fmt.Errorf("error setting uid in state: %s", err) |
| } |
| if err = d.Set("update_time", res.UpdateTime); err != nil { |
| return fmt.Errorf("error setting update_time in state: %s", err) |
| } |
| |
| return nil |
| } |
| func resourceEventarcChannelUpdate(d *schema.ResourceData, meta interface{}) error { |
| config := meta.(*transport_tpg.Config) |
| project, err := tpgresource.GetProject(d, config) |
| if err != nil { |
| return err |
| } |
| |
| obj := &eventarc.Channel{ |
| Location: dcl.String(d.Get("location").(string)), |
| Name: dcl.String(d.Get("name").(string)), |
| CryptoKeyName: dcl.String(d.Get("crypto_key_name").(string)), |
| Project: dcl.String(project), |
| ThirdPartyProvider: dcl.String(d.Get("third_party_provider").(string)), |
| } |
| directive := tpgdclresource.UpdateDirective |
| userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) |
| if err != nil { |
| return err |
| } |
| |
| billingProject := "" |
| // err == nil indicates that the billing_project value was found |
| if bp, err := tpgresource.GetBillingProject(d, config); err == nil { |
| billingProject = bp |
| } |
| client := transport_tpg.NewDCLEventarcClient(config, userAgent, billingProject, d.Timeout(schema.TimeoutUpdate)) |
| 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.ApplyChannel(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 updating Channel: %s", err) |
| } |
| |
| log.Printf("[DEBUG] Finished creating Channel %q: %#v", d.Id(), res) |
| |
| return resourceEventarcChannelRead(d, meta) |
| } |
| |
| func resourceEventarcChannelDelete(d *schema.ResourceData, meta interface{}) error { |
| config := meta.(*transport_tpg.Config) |
| project, err := tpgresource.GetProject(d, config) |
| if err != nil { |
| return err |
| } |
| |
| obj := &eventarc.Channel{ |
| Location: dcl.String(d.Get("location").(string)), |
| Name: dcl.String(d.Get("name").(string)), |
| CryptoKeyName: dcl.String(d.Get("crypto_key_name").(string)), |
| Project: dcl.String(project), |
| ThirdPartyProvider: dcl.String(d.Get("third_party_provider").(string)), |
| } |
| |
| log.Printf("[DEBUG] Deleting Channel %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.NewDCLEventarcClient(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.DeleteChannel(context.Background(), obj); err != nil { |
| return fmt.Errorf("Error deleting Channel: %s", err) |
| } |
| |
| log.Printf("[DEBUG] Finished deleting Channel %q", d.Id()) |
| return nil |
| } |
| |
| func resourceEventarcChannelImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { |
| config := meta.(*transport_tpg.Config) |
| |
| if err := tpgresource.ParseImportId([]string{ |
| "projects/(?P<project>[^/]+)/locations/(?P<location>[^/]+)/channels/(?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}}/channels/{{name}}") |
| if err != nil { |
| return nil, fmt.Errorf("Error constructing id: %s", err) |
| } |
| d.SetId(id) |
| |
| return []*schema.ResourceData{d}, nil |
| } |