Skip to content

Commit

Permalink
Revert "Update cloudbuild trigger in place (#2121)"
Browse files Browse the repository at this point in the history
This reverts commit 02eb26b.
  • Loading branch information
rileykarson committed Nov 13, 2018
1 parent c452e21 commit 2f6b39e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 116 deletions.
84 changes: 30 additions & 54 deletions google/resource_cloudbuild_build_trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ func resourceCloudBuildTrigger() *schema.Resource {
return &schema.Resource{
Create: resourceCloudbuildBuildTriggerCreate,
Read: resourceCloudbuildBuildTriggerRead,
Update: resourceCloudbuildBuildTriggerUpdate,
Delete: resourceCloudbuildBuildTriggerDelete,
Importer: &schema.ResourceImporter{
State: resourceCloudBuildTriggerImportState,
Expand All @@ -40,39 +39,46 @@ func resourceCloudBuildTrigger() *schema.Resource {
"filename": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"build"},
},
"build": {
Type: schema.TypeList,
Description: "Contents of the build template.",
Optional: true,
ForceNew: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"images": &schema.Schema{
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"step": &schema.Schema{
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"args": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
},
},
},
"tags": &schema.Schema{
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
Expand All @@ -81,41 +87,50 @@ func resourceCloudBuildTrigger() *schema.Resource {
"description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"substitutions": &schema.Schema{
Optional: true,
Type: schema.TypeMap,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"trigger_template": &schema.Schema{
Optional: true,
Type: schema.TypeList,
MaxItems: 1,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"branch_name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"commit_sha": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"dir": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"project": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"repo_name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"tag_name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
},
},
Expand All @@ -132,11 +147,22 @@ func resourceCloudbuildBuildTriggerCreate(d *schema.ResourceData, meta interface
return err
}

buildTrigger, err := expandCloudbuildBuildTrigger(d, meta)
if err != nil {
return err
// Build the address parameter
buildTrigger := &cloudbuild.BuildTrigger{}

if v, ok := d.GetOk("description"); ok {
buildTrigger.Description = v.(string)
}

if v, ok := d.GetOk("filename"); ok {
buildTrigger.Filename = v.(string)
} else {
buildTrigger.Build = expandCloudbuildBuildTriggerBuild(d)
}

buildTrigger.TriggerTemplate = expandCloudbuildBuildTriggerTemplate(d, project)
buildTrigger.Substitutions = expandStringMap(d, "substitutions")

tstr, err := json.Marshal(buildTrigger)
if err != nil {
return err
Expand Down Expand Up @@ -182,56 +208,6 @@ func resourceCloudbuildBuildTriggerRead(d *schema.ResourceData, meta interface{}
return nil
}

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

project, err := getProject(d, config)
if err != nil {
return err
}

buildTrigger, err := expandCloudbuildBuildTrigger(d, meta)
if err != nil {
return err
}

id := d.Id()

log.Printf("[INFO] Updating Cloud Build Trigger: %s", id)

if _, err = config.clientBuild.Projects.Triggers.Patch(project, id, buildTrigger).Do(); err != nil {
return err
}

return resourceCloudbuildBuildTriggerRead(d, meta)
}

func expandCloudbuildBuildTrigger(d *schema.ResourceData, meta interface{}) (*cloudbuild.BuildTrigger, error) {
config := meta.(*Config)

project, err := getProject(d, config)
if err != nil {
return nil, err
}

t := &cloudbuild.BuildTrigger{}

if v, ok := d.GetOk("description"); ok {
t.Description = v.(string)
}

if v, ok := d.GetOk("filename"); ok {
t.Filename = v.(string)
} else {
t.Build = expandCloudbuildBuildTriggerBuild(d)
}

t.Substitutions = expandStringMap(d, "substitutions")
t.TriggerTemplate = expandCloudbuildBuildTriggerTemplate(d, project)

return t, nil
}

func expandCloudbuildBuildTriggerTemplate(d *schema.ResourceData, project string) *cloudbuild.RepoSource {
if d.Get("trigger_template.#").(int) == 0 {
return nil
Expand Down
62 changes: 0 additions & 62 deletions google/resource_cloudbuild_build_trigger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,6 @@ func TestAccCloudBuildTrigger_basic(t *testing.T) {
ImportStateVerify: true,
ImportStateIdPrefix: fmt.Sprintf("%s/", projectID),
},
resource.TestStep{
Config: testGoogleCloudBuildTrigger_updated(projectID, projectOrg, projectBillingAccount),
},
resource.TestStep{
ResourceName: "google_cloudbuild_trigger.build_trigger",
ImportState: true,
ImportStateVerify: true,
ImportStateIdPrefix: fmt.Sprintf("%s/", projectID),
},
resource.TestStep{
Config: testGoogleCloudBuildTrigger_removed(projectID, projectOrg, projectBillingAccount),
Check: resource.ComposeTestCheckFunc(
Expand Down Expand Up @@ -219,59 +210,6 @@ resource "google_cloudbuild_trigger" "build_trigger" {
`, projectID, projectID, projectOrg, projectBillingAccount)
}

func testGoogleCloudBuildTrigger_updated(projectID, projectOrg, projectBillingAccount string) string {
return fmt.Sprintf(`
resource "google_project" "acceptance" {
name = "%s"
project_id = "%s"
org_id = "%s"
billing_account = "%s"
}
resource "google_project_services" "acceptance" {
project = "${google_project.acceptance.project_id}"
services = [
"cloudbuild.googleapis.com",
"containerregistry.googleapis.com",
"logging.googleapis.com",
"pubsub.googleapis.com",
"storage-api.googleapis.com",
]
}
resource "google_cloudbuild_trigger" "build_trigger" {
project = "${google_project_services.acceptance.project}"
description = "acceptance test build trigger updated"
trigger_template {
branch_name = "master-updated"
project = "${google_project_services.acceptance.project}"
repo_name = "some-repo-updated"
}
build {
images = ["gcr.io/$PROJECT_ID/$REPO_NAME:$SHORT_SHA"]
tags = ["team-a", "service-b", "updated"]
step {
name = "gcr.io/cloud-builders/gsutil"
args = "cp gs://mybucket/remotefile.zip localfile-updated.zip "
}
step {
name = "gcr.io/cloud-builders/go"
args = "build my_package_updated"
}
step {
name = "gcr.io/cloud-builders/docker"
args = "build -t gcr.io/$PROJECT_ID/$REPO_NAME:$SHORT_SHA -f Dockerfile ."
}
step {
name = "gcr.io/$PROJECT_ID/$REPO_NAME:$SHORT_SHA"
args = "test"
}
}
}
`, projectID, projectID, projectOrg, projectBillingAccount)
}

func testGoogleCloudBuildTrigger_filename(projectID, projectOrg, projectBillingAccount string) string {
return fmt.Sprintf(`
resource "google_project" "acceptance" {
Expand Down

0 comments on commit 2f6b39e

Please sign in to comment.