github.com/mponton/terratest@v0.44.0/modules/gcp/cloudbuild.go (about) 1 package gcp 2 3 import ( 4 "context" 5 "fmt" 6 7 cloudbuild "cloud.google.com/go/cloudbuild/apiv1/v2" 8 "github.com/mponton/terratest/modules/testing" 9 "github.com/stretchr/testify/require" 10 "google.golang.org/api/iterator" 11 cloudbuildpb "google.golang.org/genproto/googleapis/devtools/cloudbuild/v1" 12 ) 13 14 // CreateBuild creates a new build blocking until the operation is complete. 15 func CreateBuild(t testing.TestingT, projectID string, build *cloudbuildpb.Build) *cloudbuildpb.Build { 16 out, err := CreateBuildE(t, projectID, build) 17 require.NoError(t, err) 18 return out 19 } 20 21 // CreateBuildE creates a new build blocking until the operation is complete. 22 func CreateBuildE(t testing.TestingT, projectID string, build *cloudbuildpb.Build) (*cloudbuildpb.Build, error) { 23 ctx := context.Background() 24 25 service, err := NewCloudBuildServiceE(t) 26 if err != nil { 27 return nil, err 28 } 29 30 req := &cloudbuildpb.CreateBuildRequest{ 31 ProjectId: projectID, 32 Build: build, 33 } 34 35 op, err := service.CreateBuild(ctx, req) 36 if err != nil { 37 return nil, fmt.Errorf("CreateBuildE.CreateBuild(%s) got error: %v", projectID, err) 38 } 39 40 resp, err := op.Wait(ctx) 41 if err != nil { 42 return nil, fmt.Errorf("CreateBuildE.Wait(%s) got error: %v", projectID, err) 43 } 44 45 return resp, nil 46 } 47 48 // GetBuild gets the given build. 49 func GetBuild(t testing.TestingT, projectID string, buildID string) *cloudbuildpb.Build { 50 out, err := GetBuildE(t, projectID, buildID) 51 require.NoError(t, err) 52 return out 53 } 54 55 // GetBuildE gets the given build. 56 func GetBuildE(t testing.TestingT, projectID string, buildID string) (*cloudbuildpb.Build, error) { 57 ctx := context.Background() 58 59 service, err := NewCloudBuildServiceE(t) 60 if err != nil { 61 return nil, err 62 } 63 64 req := &cloudbuildpb.GetBuildRequest{ 65 ProjectId: projectID, 66 Id: buildID, 67 } 68 69 resp, err := service.GetBuild(ctx, req) 70 if err != nil { 71 return nil, fmt.Errorf("GetBuildE.GetBuild(%s, %s) got error: %v", projectID, buildID, err) 72 } 73 74 return resp, nil 75 } 76 77 // GetBuilds gets the list of builds for a given project. 78 func GetBuilds(t testing.TestingT, projectID string) []*cloudbuildpb.Build { 79 out, err := GetBuildsE(t, projectID) 80 require.NoError(t, err) 81 return out 82 } 83 84 // GetBuildsE gets the list of builds for a given project. 85 func GetBuildsE(t testing.TestingT, projectID string) ([]*cloudbuildpb.Build, error) { 86 ctx := context.Background() 87 88 service, err := NewCloudBuildServiceE(t) 89 if err != nil { 90 return nil, err 91 } 92 93 req := &cloudbuildpb.ListBuildsRequest{ 94 ProjectId: projectID, 95 } 96 97 it := service.ListBuilds(ctx, req) 98 builds := []*cloudbuildpb.Build{} 99 100 for { 101 resp, err := it.Next() 102 if err == iterator.Done { 103 break 104 } 105 if err != nil { 106 return nil, fmt.Errorf("GetBuildsE.ListBuilds(%s) got error: %v", projectID, err) 107 } 108 109 builds = append(builds, resp) 110 } 111 112 return builds, nil 113 } 114 115 // GetBuildsForTrigger gets a list of builds for a specific cloud build trigger. 116 func GetBuildsForTrigger(t testing.TestingT, projectID string, triggerID string) []*cloudbuildpb.Build { 117 out, err := GetBuildsForTriggerE(t, projectID, triggerID) 118 require.NoError(t, err) 119 return out 120 } 121 122 // GetBuildsForTriggerE gets a list of builds for a specific cloud build trigger. 123 func GetBuildsForTriggerE(t testing.TestingT, projectID string, triggerID string) ([]*cloudbuildpb.Build, error) { 124 builds, err := GetBuildsE(t, projectID) 125 if err != nil { 126 return nil, fmt.Errorf("GetBuildsE.ListBuilds(%s) got error: %v", projectID, err) 127 } 128 129 filteredBuilds := []*cloudbuildpb.Build{} 130 for _, build := range builds { 131 if build.GetBuildTriggerId() == triggerID { 132 filteredBuilds = append(filteredBuilds, build) 133 } 134 } 135 136 return filteredBuilds, nil 137 } 138 139 // NewCloudBuildService creates a new Cloud Build service, which is used to make Cloud Build API calls. 140 func NewCloudBuildService(t testing.TestingT) *cloudbuild.Client { 141 service, err := NewCloudBuildServiceE(t) 142 require.NoError(t, err) 143 return service 144 } 145 146 // NewCloudBuildServiceE creates a new Cloud Build service, which is used to make Cloud Build API calls. 147 func NewCloudBuildServiceE(t testing.TestingT) (*cloudbuild.Client, error) { 148 ctx := context.Background() 149 150 service, err := cloudbuild.NewClient(ctx) 151 if err != nil { 152 return nil, err 153 } 154 155 return service, nil 156 }