Skip to content

Commit

Permalink
Adding Eventarc Channel Resource support for TPG (#6529) (#13021)
Browse files Browse the repository at this point in the history
Co-authored-by: Riley Karson <rileykarson@google.com>
Fixes #12517

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 11, 2022
1 parent 64819cf commit bc240c0
Show file tree
Hide file tree
Showing 10 changed files with 965 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changelog/6529.txt
@@ -0,0 +1,6 @@
```release-note:new-resource
google_eventarc_channel
```
```release-note:enhancement
google_eventarc_trigger: Added field `channel` to `google_eventarc_trigger`
```
1 change: 1 addition & 0 deletions google/provider_dcl_resources.go
Expand Up @@ -42,6 +42,7 @@ var dclResources = map[string]*schema.Resource{
"google_dataplex_lake": resourceDataplexLake(),
"google_dataplex_zone": resourceDataplexZone(),
"google_dataproc_workflow_template": resourceDataprocWorkflowTemplate(),
"google_eventarc_channel": resourceEventarcChannel(),
"google_eventarc_trigger": resourceEventarcTrigger(),
"google_firebaserules_release": resourceFirebaserulesRelease(),
"google_firebaserules_ruleset": resourceFirebaserulesRuleset(),
Expand Down
355 changes: 355 additions & 0 deletions google/resource_eventarc_channel.go
@@ -0,0 +1,355 @@
// ----------------------------------------------------------------------------
//
// *** 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 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),
},

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: 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",
},

"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.(*Config)
project, err := 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 := CreateDirective
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.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.(*Config)
project, err := 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 := 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.GetChannel(context.Background(), obj)
if err != nil {
resourceName := fmt.Sprintf("EventarcChannel %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("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.(*Config)
project, err := 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 := 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.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.(*Config)
project, err := 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 := 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.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.(*Config)

if err := 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 := 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
}

0 comments on commit bc240c0

Please sign in to comment.