Skip to content

Commit

Permalink
Adding Eventarc GoogleChannelConfig Resource support for TPG (#6534) (#…
Browse files Browse the repository at this point in the history
…13080)

Fixes #12520

Signed-off-by: Modular Magician <magic-modules@google.com>

Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician committed Nov 18, 2022
1 parent 85eb4f6 commit 7da3ffc
Show file tree
Hide file tree
Showing 5 changed files with 609 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/6534.txt
@@ -0,0 +1,3 @@
```release-note:new-resource
google_eventarc_google_channel_config
```
1 change: 1 addition & 0 deletions google/provider_dcl_resources.go
Expand Up @@ -45,6 +45,7 @@ var dclResources = map[string]*schema.Resource{
"google_dataplex_zone": resourceDataplexZone(),
"google_dataproc_workflow_template": resourceDataprocWorkflowTemplate(),
"google_eventarc_channel": resourceEventarcChannel(),
"google_eventarc_google_channel_config": resourceEventarcGoogleChannelConfig(),
"google_eventarc_trigger": resourceEventarcTrigger(),
"google_firebaserules_release": resourceFirebaserulesRelease(),
"google_firebaserules_ruleset": resourceFirebaserulesRuleset(),
Expand Down
297 changes: 297 additions & 0 deletions google/resource_eventarc_google_channel_config.go
@@ -0,0 +1,297 @@
// ----------------------------------------------------------------------------
//
// *** 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 google

import (
"context"
"fmt"
"log"
"time"

"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"
)

func resourceEventarcGoogleChannelConfig() *schema.Resource {
return &schema.Resource{
Create: resourceEventarcGoogleChannelConfigCreate,
Read: resourceEventarcGoogleChannelConfigRead,
Update: resourceEventarcGoogleChannelConfigUpdate,
Delete: resourceEventarcGoogleChannelConfigDelete,

Importer: &schema.ResourceImporter{
State: resourceEventarcGoogleChannelConfigImport,
},

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(20 * time.Minute),
Update: schema.DefaultTimeout(20 * time.Minute),
Delete: schema.DefaultTimeout(20 * time.Minute),
},

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 config. Must be in the format of, `projects/{project}/locations/{location}/googleChannelConfig`.",
},

"crypto_key_name": {
Type: schema.TypeString,
Optional: true,
DiffSuppressFunc: 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: compareSelfLinkOrResourceName,
Description: "The project for the resource",
},

"update_time": {
Type: schema.TypeString,
Computed: true,
Description: "Output only. The last-modified time.",
},
},
}
}

func resourceEventarcGoogleChannelConfigCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}

obj := &eventarc.GoogleChannelConfig{
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),
}

id, err := obj.ID()
if err != nil {
return fmt.Errorf("error constructing id: %s", err)
}
d.SetId(id)

directive := UpdateDirective
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
billingProject := project
// err == nil indicates that the billing_project value was found
if bp, err := getBillingProject(d, config); err == nil {
billingProject = bp
}
client := NewDCLEventarcClient(config, userAgent, billingProject, d.Timeout(schema.TimeoutCreate))
if bp, err := 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.ApplyGoogleChannelConfig(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 GoogleChannelConfig: %s", err)
}

log.Printf("[DEBUG] Finished creating GoogleChannelConfig %q: %#v", d.Id(), res)

return resourceEventarcGoogleChannelConfigRead(d, meta)
}

func resourceEventarcGoogleChannelConfigRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}

obj := &eventarc.GoogleChannelConfig{
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),
}

userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
billingProject := project
// err == nil indicates that the billing_project value was found
if bp, err := getBillingProject(d, config); err == nil {
billingProject = bp
}
client := NewDCLEventarcClient(config, userAgent, billingProject, d.Timeout(schema.TimeoutRead))
if bp, err := 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.GetGoogleChannelConfig(context.Background(), obj)
if err != nil {
resourceName := fmt.Sprintf("EventarcGoogleChannelConfig %q", d.Id())
return 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("update_time", res.UpdateTime); err != nil {
return fmt.Errorf("error setting update_time in state: %s", err)
}

return nil
}
func resourceEventarcGoogleChannelConfigUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}

obj := &eventarc.GoogleChannelConfig{
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),
}
directive := UpdateDirective
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}

billingProject := ""
// err == nil indicates that the billing_project value was found
if bp, err := getBillingProject(d, config); err == nil {
billingProject = bp
}
client := NewDCLEventarcClient(config, userAgent, billingProject, d.Timeout(schema.TimeoutUpdate))
if bp, err := 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.ApplyGoogleChannelConfig(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 GoogleChannelConfig: %s", err)
}

log.Printf("[DEBUG] Finished creating GoogleChannelConfig %q: %#v", d.Id(), res)

return resourceEventarcGoogleChannelConfigRead(d, meta)
}

func resourceEventarcGoogleChannelConfigDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}

obj := &eventarc.GoogleChannelConfig{
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),
}

log.Printf("[DEBUG] Deleting GoogleChannelConfig %q", d.Id())
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
billingProject := project
// err == nil indicates that the billing_project value was found
if bp, err := getBillingProject(d, config); err == nil {
billingProject = bp
}
client := NewDCLEventarcClient(config, userAgent, billingProject, d.Timeout(schema.TimeoutDelete))
if bp, err := 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.DeleteGoogleChannelConfig(context.Background(), obj); err != nil {
return fmt.Errorf("Error deleting GoogleChannelConfig: %s", err)
}

log.Printf("[DEBUG] Finished deleting GoogleChannelConfig %q", d.Id())
return nil
}

func resourceEventarcGoogleChannelConfigImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
config := meta.(*Config)

if err := parseImportId([]string{
"projects/(?P<project>[^/]+)/locations/(?P<location>[^/]+)/googleChannelConfig",
"(?P<project>[^/]+)/(?P<location>[^/]+)",
"(?P<location>[^/]+)",
}, d, config); err != nil {
return nil, err
}

// Replace import id for the resource id
id, err := replaceVarsForId(d, config, "projects/{{project}}/locations/{{location}}/googleChannelConfig")
if err != nil {
return nil, fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)

return []*schema.ResourceData{d}, nil
}

0 comments on commit 7da3ffc

Please sign in to comment.