Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cloudbuild worker pools #4877

Merged
merged 13 commits into from
Jun 22, 2021
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
<% autogen_exception -%>
package google

<% unless version == 'ga' -%>
import (
"fmt"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func TestAccCloudbuildWorkerPool_basic(t *testing.T) {
t.Parallel()

context := map[string]interface{}{
"random_suffix": randString(t, 10),
"project": getTestProjectFromEnv(),
}

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: funcAccTestCloudbuildWorkerPoolCheckDestroy(t),
Steps: []resource.TestStep{
{
Config: testAccCloudbuildWorkerPool_basic(context),
},
{
ImportState: true,
ImportStateVerify: true,
ResourceName: "google_cloudbuild_worker_pool.pool",
},
{
Config: testAccCloudbuildWorkerPool_updated(context),
},
{
ImportState: true,
ImportStateVerify: true,
ResourceName: "google_cloudbuild_worker_pool.pool",
},
{
Config: testAccCloudbuildWorkerPool_noWorkerConfig(context),
},
{
ImportState: true,
ImportStateVerify: true,
ResourceName: "google_cloudbuild_worker_pool.pool",
},
},
})
}

func testAccCloudbuildWorkerPool_basic(context map[string]interface{}) string {
return Nprintf(`
resource "google_cloudbuild_worker_pool" "pool" {
name = "pool%{random_suffix}"
location = "europe-west1"
worker_config {
disk_size_gb = 100
machine_type = "e2-standard-8"
no_external_ip = true
}
}
`, context)
}

func testAccCloudbuildWorkerPool_updated(context map[string]interface{}) string {
return Nprintf(`
resource "google_cloudbuild_worker_pool" "pool" {
name = "pool%{random_suffix}"
location = "europe-west1"
worker_config {
disk_size_gb = 101
machine_type = "e2-standard-4"
slevenick marked this conversation as resolved.
Show resolved Hide resolved
no_external_ip = false
}
}
`, context)
}

func testAccCloudbuildWorkerPool_noWorkerConfig(context map[string]interface{}) string {
return Nprintf(`
resource "google_cloudbuild_worker_pool" "pool" {
name = "pool%{random_suffix}"
location = "europe-west1"
}
`, context)
}

func TestAccCloudbuildWorkerPool_withNetwork(t *testing.T) {
t.Parallel()

context := map[string]interface{}{
"random_suffix": randString(t, 10),
"project": getTestProjectFromEnv(),
}

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: funcAccTestCloudbuildWorkerPoolCheckDestroy(t),
Steps: []resource.TestStep{
{
Config: testAccCloudbuildWorkerPool_withNetwork(context),
},
{
ImportState: true,
ImportStateVerify: true,
ResourceName: "google_cloudbuild_worker_pool.pool",
},
},
})
}

func testAccCloudbuildWorkerPool_withNetwork(context map[string]interface{}) string {
return Nprintf(`
resource "google_project_service" "servicenetworking" {
service = "servicenetworking.googleapis.com"
disable_on_destroy = false
}

resource "google_compute_network" "network" {
name = "tf-test-network%{random_suffix}"
auto_create_subnetworks = false
depends_on = [google_project_service.servicenetworking]
}

resource "google_compute_global_address" "worker_range" {
name = "worker-pool-range"
purpose = "VPC_PEERING"
address_type = "INTERNAL"
prefix_length = 16
network = google_compute_network.network.id
}

resource "google_service_networking_connection" "worker_pool_conn" {
network = google_compute_network.network.id
service = "servicenetworking.googleapis.com"
reserved_peering_ranges = [google_compute_global_address.worker_range.name]
depends_on = [google_project_service.servicenetworking]
}

resource "google_cloudbuild_worker_pool" "pool" {
name = "pool%{random_suffix}"
location = "europe-west1"
worker_config {
disk_size_gb = 101
machine_type = "e2-standard-4"
no_external_ip = false
}
network_config {
peered_network = google_compute_network.network.id
}
depends_on = [google_service_networking_connection.worker_pool_conn]
}
`, context)
}

func funcAccTestCloudbuildWorkerPoolCheckDestroy(t *testing.T) func(s *terraform.State) error {
return func(s *terraform.State) error {
for name, rs := range s.RootModule().Resources {
if rs.Type != "google_cloudbuild_worker_pool" {
continue
}
if strings.HasPrefix(name, "data.") {
continue
}

config := googleProviderConfig(t)

url, err := replaceVarsForTest(config, rs, "{{CloudBuildBasePath}}projects/{{project}}/locations/{{location}}/workerPools/{{name}}")
if err != nil {
return err
}

billingProject := ""

if config.BillingProject != "" {
billingProject = config.BillingProject
}

_, err = sendRequest(config, "GET", billingProject, url, config.userAgent, nil)
if err == nil {
return fmt.Errorf("CloudbuildWorkerPool still exists at %s", url)
}
}

return nil
}
}
<% end -%>
7 changes: 7 additions & 0 deletions mmv1/third_party/terraform/utils/config.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ import (
"google.golang.org/api/storagetransfer/v1"
"google.golang.org/api/transport"
dcl "github.com/GoogleCloudPlatform/declarative-resource-client-library/dcl"
<% unless version == 'ga' -%>
cloudbuildDcl "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/cloudbuild/beta"
<% end -%>
dataprocDcl "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/dataproc<% unless version == 'ga' -%>/beta<% end -%>"
eventarcDcl "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/eventarc<% unless version == 'ga' -%>/beta<% end -%>"
<% unless version == 'ga' -%>
Expand Down Expand Up @@ -126,6 +129,10 @@ type Config struct {
// dataprocBasePath is implemented in mm
EventarcBasePath string
GkeHubBasePath string
<% unless version == 'ga' -%>
// CloudBuild WorkerPool uses a different endpoint (v1beta1) than any other CloudBuild resources
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As of the GA release of Worker Pools, this is no longer the case.
I'm happy to assist if this is not priority, but it appears some work is also needed in declarative-resource-client-library? @slevenick let me know if this is something that can be tackled outside Google!

CloudBuildWorkerPoolBasePath string
<% end -%>
}

<% products.each do |product| -%>
Expand Down
9 changes: 9 additions & 0 deletions mmv1/third_party/terraform/utils/provider.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ func Provider() *schema.Provider {
// dcl
EventarcEndpointEntryKey: EventarcEndpointEntry,
GkeHubFeatureCustomEndpointEntryKey: GkeHubFeatureCustomEndpointEntry,
<% unless version == "ga" -%>
CloudBuildWorkerPoolEndpointEntryKey: CloudBuildWorkerPoolEndpointEntry,
<% end -%>
},

ProviderMetaSchema: map[string]*schema.Schema{
Expand Down Expand Up @@ -350,6 +353,9 @@ end # products.each do
"google_bigtable_instance": resourceBigtableInstance(),
"google_bigtable_table": resourceBigtableTable(),
"google_billing_subaccount": resourceBillingSubaccount(),
<% unless version == 'ga' -%>
"google_cloudbuild_worker_pool": resourceCloudbuildWorkerPool(),
<% end -%>
"google_cloudfunctions_function": resourceCloudFunctionsFunction(),
"google_composer_environment": resourceComposerEnvironment(),
"google_compute_attached_disk": resourceComputeAttachedDisk(),
Expand Down Expand Up @@ -591,6 +597,9 @@ func providerConfigure(ctx context.Context, d *schema.ResourceData, p *schema.Pr
// dcl
config.EventarcBasePath = d.Get(EventarcEndpointEntryKey).(string)
config.GkeHubBasePath = d.Get(GkeHubFeatureCustomEndpointEntryKey).(string)
<% unless version == "ga" -%>
config.CloudBuildWorkerPoolBasePath = d.Get(CloudBuildWorkerPoolEndpointEntryKey).(string)
<% end -%>

stopCtx, ok := schema.StopContext(ctx)
if !ok {
Expand Down
Loading