Skip to content

Commit

Permalink
Add GCE Zonal Reservation resource (#1086)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored and rileykarson committed Sep 16, 2019
1 parent 0c4851b commit 4e94744
Show file tree
Hide file tree
Showing 7 changed files with 1,166 additions and 2 deletions.
5 changes: 3 additions & 2 deletions google-beta/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,9 +464,9 @@ func Provider() terraform.ResourceProvider {
return provider
}

// Generated resources: 95
// Generated resources: 96
// Generated IAM resources: 21
// Total generated resources: 116
// Total generated resources: 117
func ResourceMap() map[string]*schema.Resource {
resourceMap, _ := ResourceMapWithErrors()
return resourceMap
Expand Down Expand Up @@ -524,6 +524,7 @@ func ResourceMapWithErrors() (map[string]*schema.Resource, error) {
"google_compute_snapshot": resourceComputeSnapshot(),
"google_compute_ssl_certificate": resourceComputeSslCertificate(),
"google_compute_managed_ssl_certificate": resourceComputeManagedSslCertificate(),
"google_compute_reservation": resourceComputeReservation(),
"google_compute_ssl_policy": resourceComputeSslPolicy(),
"google_compute_subnetwork": resourceComputeSubnetwork(),
"google_compute_target_http_proxy": resourceComputeTargetHttpProxy(),
Expand Down
90 changes: 90 additions & 0 deletions google-beta/resource_compute_backend_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,24 @@ func resourceComputeBackendService() *schema.Resource {
ValidateFunc: validation.StringInSlice([]string{"EXTERNAL", "INTERNAL_SELF_MANAGED", ""}, false),
Default: "EXTERNAL",
},
"log_config": {
Type: schema.TypeList,
Computed: true,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enable": {
Type: schema.TypeBool,
Optional: true,
},
"sample_rate": {
Type: schema.TypeFloat,
Optional: true,
},
},
},
},
"port_name": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -493,6 +511,12 @@ func resourceComputeBackendServiceCreate(d *schema.ResourceData, meta interface{
} else if v, ok := d.GetOkExists("timeout_sec"); !isEmptyValue(reflect.ValueOf(timeoutSecProp)) && (ok || !reflect.DeepEqual(v, timeoutSecProp)) {
obj["timeoutSec"] = timeoutSecProp
}
logConfigProp, err := expandComputeBackendServiceLogConfig(d.Get("log_config"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("log_config"); !isEmptyValue(reflect.ValueOf(logConfigProp)) && (ok || !reflect.DeepEqual(v, logConfigProp)) {
obj["logConfig"] = logConfigProp
}

obj, err = resourceComputeBackendServiceEncoder(d, meta, obj)
if err != nil {
Expand Down Expand Up @@ -655,6 +679,9 @@ func resourceComputeBackendServiceRead(d *schema.ResourceData, meta interface{})
if err := d.Set("timeout_sec", flattenComputeBackendServiceTimeoutSec(res["timeoutSec"], d)); err != nil {
return fmt.Errorf("Error reading BackendService: %s", err)
}
if err := d.Set("log_config", flattenComputeBackendServiceLogConfig(res["logConfig"], d)); err != nil {
return fmt.Errorf("Error reading BackendService: %s", err)
}
if err := d.Set("self_link", ConvertSelfLinkToV1(res["selfLink"].(string))); err != nil {
return fmt.Errorf("Error reading BackendService: %s", err)
}
Expand Down Expand Up @@ -773,6 +800,12 @@ func resourceComputeBackendServiceUpdate(d *schema.ResourceData, meta interface{
} else if v, ok := d.GetOkExists("timeout_sec"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, timeoutSecProp)) {
obj["timeoutSec"] = timeoutSecProp
}
logConfigProp, err := expandComputeBackendServiceLogConfig(d.Get("log_config"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("log_config"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, logConfigProp)) {
obj["logConfig"] = logConfigProp
}

obj, err = resourceComputeBackendServiceEncoder(d, meta, obj)
if err != nil {
Expand Down Expand Up @@ -1182,6 +1215,29 @@ func flattenComputeBackendServiceTimeoutSec(v interface{}, d *schema.ResourceDat
return v
}

func flattenComputeBackendServiceLogConfig(v interface{}, d *schema.ResourceData) interface{} {
if v == nil {
return nil
}
original := v.(map[string]interface{})
if len(original) == 0 {
return nil
}
transformed := make(map[string]interface{})
transformed["enable"] =
flattenComputeBackendServiceLogConfigEnable(original["enable"], d)
transformed["sample_rate"] =
flattenComputeBackendServiceLogConfigSampleRate(original["sampleRate"], d)
return []interface{}{transformed}
}
func flattenComputeBackendServiceLogConfigEnable(v interface{}, d *schema.ResourceData) interface{} {
return v
}

func flattenComputeBackendServiceLogConfigSampleRate(v interface{}, d *schema.ResourceData) interface{} {
return v
}

func expandComputeBackendServiceAffinityCookieTtlSec(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
Expand Down Expand Up @@ -1533,6 +1589,40 @@ func expandComputeBackendServiceTimeoutSec(v interface{}, d TerraformResourceDat
return v, nil
}

func expandComputeBackendServiceLogConfig(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{})

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

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

return transformed, nil
}

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

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

func resourceComputeBackendServiceEncoder(d *schema.ResourceData, meta interface{}, obj map[string]interface{}) (map[string]interface{}, error) {
// The BackendService API's Update / PUT API is badly formed and behaves like
// a PATCH field for at least IAP. When sent a `null` `iap` field, the API
Expand Down
52 changes: 52 additions & 0 deletions google-beta/resource_compute_backend_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,37 @@ func TestAccComputeBackendService_internalLoadBalancing(t *testing.T) {
})
}

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

serviceName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
checkName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeBackendServiceDestroy,
Steps: []resource.TestStep{
{
Config: testAccComputeBackendService_withLogConfig(serviceName, checkName, 0.7),
},
{
ResourceName: "google_compute_backend_service.foobar",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccComputeBackendService_withLogConfig(serviceName, checkName, 0.4),
},
{
ResourceName: "google_compute_backend_service.foobar",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccComputeBackendService_basic(serviceName, checkName string) string {
return fmt.Sprintf(`
resource "google_compute_backend_service" "foobar" {
Expand Down Expand Up @@ -1422,3 +1453,24 @@ resource "google_compute_instance_template" "foobar" {
}
}`, fr, proxy, backend, hc, urlmap)
}

func testAccComputeBackendService_withLogConfig(serviceName, checkName string, sampleRate float64) string {
return fmt.Sprintf(`
resource "google_compute_backend_service" "foobar" {
name = "%s"
health_checks = ["${google_compute_http_health_check.zero.self_link}"]
log_config {
enable = true
sample_rate = %v
}
}
resource "google_compute_http_health_check" "zero" {
name = "%s"
request_path = "/"
check_interval_sec = 1
timeout_sec = 1
}
`, serviceName, sampleRate, checkName)
}
Loading

0 comments on commit 4e94744

Please sign in to comment.