Skip to content

Commit

Permalink
Added pubsubConfig and webhookConfig support to the cloud build resou…
Browse files Browse the repository at this point in the history
…rce. (#4931) (#9541)

Co-authored-by: Sumit Madan <[email protected]>
Signed-off-by: Modular Magician <[email protected]>

Co-authored-by: Sumit Madan <[email protected]>
  • Loading branch information
modular-magician and iamsumit authored Jul 9, 2021
1 parent a60eda5 commit 2854a21
Show file tree
Hide file tree
Showing 4 changed files with 532 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .changelog/4931.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
cloudbuild: Added `pubsub_config` and `webhook_config` parameter to `google_cloudbuild_trigger`.
```
245 changes: 242 additions & 3 deletions google/resource_cloudbuild_trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ Default time is ten minutes (600s).`,
Optional: true,
Description: `Describes the configuration of a trigger that creates a build whenever a GitHub event is received.
One of 'trigger_template' or 'github' must be provided.`,
One of 'trigger_template', 'github', 'pubsub_config' or 'webhook_config' must be provided.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -721,7 +721,7 @@ https://github.com/googlecloudplatform/cloud-builders is "googlecloudplatform".`
},
},
},
ExactlyOneOf: []string{"trigger_template", "github"},
ExactlyOneOf: []string{"trigger_template", "github", "pubsub_config", "webhook_config"},
},
"ignored_files": {
Type: schema.TypeList,
Expand Down Expand Up @@ -763,6 +763,41 @@ a build.`,
Optional: true,
Description: `Name of the trigger. Must be unique within the project.`,
},
"pubsub_config": {
Type: schema.TypeList,
Optional: true,
Description: `PubsubConfig describes the configuration of a trigger that creates
a build whenever a Pub/Sub message is published.
One of 'trigger_template', 'github', 'pubsub_config' or 'webhook_config' must be provided.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"topic": {
Type: schema.TypeString,
Required: true,
Description: `The name of the topic from which this subscription is receiving messages.`,
},
"service_account_email": {
Type: schema.TypeString,
Optional: true,
Description: `Service account that will make the push request.`,
},
"state": {
Type: schema.TypeString,
Computed: true,
Description: `Potential issues with the underlying Pub/Sub subscription configuration.
Only populated on get requests.`,
},
"subscription": {
Type: schema.TypeString,
Computed: true,
Description: `Output only. Name of the subscription.`,
},
},
},
ExactlyOneOf: []string{"trigger_template", "github", "pubsub_config", "webhook_config"},
},
"substitutions": {
Type: schema.TypeMap,
Optional: true,
Expand All @@ -786,7 +821,7 @@ Branch and tag names in trigger templates are interpreted as regular
expressions. Any branch or tag change that matches that regular
expression will trigger a build.
One of 'trigger_template' or 'github' must be provided.`,
One of 'trigger_template', 'github', 'pubsub_config' or 'webhook_config' must be provided.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -839,6 +874,32 @@ This field is a regular expression.`,
},
},
},
ExactlyOneOf: []string{"trigger_template", "github", "pubsub_config", "webhook_config"},
},
"webhook_config": {
Type: schema.TypeList,
Optional: true,
Description: `WebhookConfig describes the configuration of a trigger that creates
a build whenever a webhook is sent to a trigger's webhook URL.
One of 'trigger_template', 'github', 'pubsub_config' or 'webhook_config' must be provided.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"secret": {
Type: schema.TypeString,
Required: true,
Description: `Resource name for the secret required as a URL parameter.`,
},
"state": {
Type: schema.TypeString,
Computed: true,
Description: `Potential issues with the underlying Pub/Sub subscription configuration.
Only populated on get requests.`,
},
},
},
ExactlyOneOf: []string{"trigger_template", "github", "pubsub_config", "webhook_config"},
},
"create_time": {
Type: schema.TypeString,
Expand Down Expand Up @@ -929,6 +990,18 @@ func resourceCloudBuildTriggerCreate(d *schema.ResourceData, meta interface{}) e
} else if v, ok := d.GetOkExists("github"); !isEmptyValue(reflect.ValueOf(githubProp)) && (ok || !reflect.DeepEqual(v, githubProp)) {
obj["github"] = githubProp
}
pubsubConfigProp, err := expandCloudBuildTriggerPubsubConfig(d.Get("pubsub_config"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("pubsub_config"); !isEmptyValue(reflect.ValueOf(pubsubConfigProp)) && (ok || !reflect.DeepEqual(v, pubsubConfigProp)) {
obj["pubsubConfig"] = pubsubConfigProp
}
webhookConfigProp, err := expandCloudBuildTriggerWebhookConfig(d.Get("webhook_config"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("webhook_config"); !isEmptyValue(reflect.ValueOf(webhookConfigProp)) && (ok || !reflect.DeepEqual(v, webhookConfigProp)) {
obj["webhookConfig"] = webhookConfigProp
}
buildProp, err := expandCloudBuildTriggerBuild(d.Get("build"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -1059,6 +1132,12 @@ func resourceCloudBuildTriggerRead(d *schema.ResourceData, meta interface{}) err
if err := d.Set("github", flattenCloudBuildTriggerGithub(res["github"], d, config)); err != nil {
return fmt.Errorf("Error reading Trigger: %s", err)
}
if err := d.Set("pubsub_config", flattenCloudBuildTriggerPubsubConfig(res["pubsubConfig"], d, config)); err != nil {
return fmt.Errorf("Error reading Trigger: %s", err)
}
if err := d.Set("webhook_config", flattenCloudBuildTriggerWebhookConfig(res["webhookConfig"], d, config)); err != nil {
return fmt.Errorf("Error reading Trigger: %s", err)
}
if err := d.Set("build", flattenCloudBuildTriggerBuild(res["build"], d, config)); err != nil {
return fmt.Errorf("Error reading Trigger: %s", err)
}
Expand Down Expand Up @@ -1142,6 +1221,18 @@ func resourceCloudBuildTriggerUpdate(d *schema.ResourceData, meta interface{}) e
} else if v, ok := d.GetOkExists("github"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, githubProp)) {
obj["github"] = githubProp
}
pubsubConfigProp, err := expandCloudBuildTriggerPubsubConfig(d.Get("pubsub_config"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("pubsub_config"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, pubsubConfigProp)) {
obj["pubsubConfig"] = pubsubConfigProp
}
webhookConfigProp, err := expandCloudBuildTriggerWebhookConfig(d.Get("webhook_config"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("webhook_config"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, webhookConfigProp)) {
obj["webhookConfig"] = webhookConfigProp
}
buildProp, err := expandCloudBuildTriggerBuild(d.Get("build"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -1408,6 +1499,64 @@ func flattenCloudBuildTriggerGithubPushTag(v interface{}, d *schema.ResourceData
return v
}

func flattenCloudBuildTriggerPubsubConfig(v interface{}, d *schema.ResourceData, config *Config) interface{} {
if v == nil {
return nil
}
original := v.(map[string]interface{})
if len(original) == 0 {
return nil
}
transformed := make(map[string]interface{})
transformed["subscription"] =
flattenCloudBuildTriggerPubsubConfigSubscription(original["subscription"], d, config)
transformed["topic"] =
flattenCloudBuildTriggerPubsubConfigTopic(original["topic"], d, config)
transformed["service_account_email"] =
flattenCloudBuildTriggerPubsubConfigServiceAccountEmail(original["service_account_email"], d, config)
transformed["state"] =
flattenCloudBuildTriggerPubsubConfigState(original["state"], d, config)
return []interface{}{transformed}
}
func flattenCloudBuildTriggerPubsubConfigSubscription(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}

func flattenCloudBuildTriggerPubsubConfigTopic(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}

func flattenCloudBuildTriggerPubsubConfigServiceAccountEmail(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}

func flattenCloudBuildTriggerPubsubConfigState(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}

func flattenCloudBuildTriggerWebhookConfig(v interface{}, d *schema.ResourceData, config *Config) interface{} {
if v == nil {
return nil
}
original := v.(map[string]interface{})
if len(original) == 0 {
return nil
}
transformed := make(map[string]interface{})
transformed["secret"] =
flattenCloudBuildTriggerWebhookConfigSecret(original["secret"], d, config)
transformed["state"] =
flattenCloudBuildTriggerWebhookConfigState(original["state"], d, config)
return []interface{}{transformed}
}
func flattenCloudBuildTriggerWebhookConfigSecret(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}

func flattenCloudBuildTriggerWebhookConfigState(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}

func flattenCloudBuildTriggerBuild(v interface{}, d *schema.ResourceData, config *Config) interface{} {
if v == nil {
return nil
Expand Down Expand Up @@ -2142,6 +2291,96 @@ func expandCloudBuildTriggerGithubPushTag(v interface{}, d TerraformResourceData
return v, nil
}

func expandCloudBuildTriggerPubsubConfig(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
return nil, nil
}
raw := l[0]
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})

transformedSubscription, err := expandCloudBuildTriggerPubsubConfigSubscription(original["subscription"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedSubscription); val.IsValid() && !isEmptyValue(val) {
transformed["subscription"] = transformedSubscription
}

transformedTopic, err := expandCloudBuildTriggerPubsubConfigTopic(original["topic"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedTopic); val.IsValid() && !isEmptyValue(val) {
transformed["topic"] = transformedTopic
}

transformedServiceAccountEmail, err := expandCloudBuildTriggerPubsubConfigServiceAccountEmail(original["service_account_email"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedServiceAccountEmail); val.IsValid() && !isEmptyValue(val) {
transformed["service_account_email"] = transformedServiceAccountEmail
}

transformedState, err := expandCloudBuildTriggerPubsubConfigState(original["state"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedState); val.IsValid() && !isEmptyValue(val) {
transformed["state"] = transformedState
}

return transformed, nil
}

func expandCloudBuildTriggerPubsubConfigSubscription(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandCloudBuildTriggerPubsubConfigTopic(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandCloudBuildTriggerPubsubConfigServiceAccountEmail(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandCloudBuildTriggerPubsubConfigState(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandCloudBuildTriggerWebhookConfig(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
return nil, nil
}
raw := l[0]
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})

transformedSecret, err := expandCloudBuildTriggerWebhookConfigSecret(original["secret"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedSecret); val.IsValid() && !isEmptyValue(val) {
transformed["secret"] = transformedSecret
}

transformedState, err := expandCloudBuildTriggerWebhookConfigState(original["state"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedState); val.IsValid() && !isEmptyValue(val) {
transformed["state"] = transformedState
}

return transformed, nil
}

func expandCloudBuildTriggerWebhookConfigSecret(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandCloudBuildTriggerWebhookConfigState(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandCloudBuildTriggerBuild(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
Expand Down
Loading

0 comments on commit 2854a21

Please sign in to comment.