Skip to content

Commit

Permalink
Add backend bucket signed URL key (for CDN) support
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
emilymye authored and modular-magician committed Mar 28, 2019
1 parent 232d09d commit f8c5a28
Show file tree
Hide file tree
Showing 7 changed files with 657 additions and 86 deletions.
59 changes: 30 additions & 29 deletions google-beta/provider_compute_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,34 @@ package google
import "github.com/hashicorp/terraform/helper/schema"

var GeneratedComputeResourcesMap = map[string]*schema.Resource{
"google_compute_address": resourceComputeAddress(),
"google_compute_autoscaler": resourceComputeAutoscaler(),
"google_compute_backend_bucket": resourceComputeBackendBucket(),
"google_compute_disk": resourceComputeDisk(),
"google_compute_firewall": resourceComputeFirewall(),
"google_compute_forwarding_rule": resourceComputeForwardingRule(),
"google_compute_global_address": resourceComputeGlobalAddress(),
"google_compute_http_health_check": resourceComputeHttpHealthCheck(),
"google_compute_https_health_check": resourceComputeHttpsHealthCheck(),
"google_compute_health_check": resourceComputeHealthCheck(),
"google_compute_image": resourceComputeImage(),
"google_compute_interconnect_attachment": resourceComputeInterconnectAttachment(),
"google_compute_network": resourceComputeNetwork(),
"google_compute_region_autoscaler": resourceComputeRegionAutoscaler(),
"google_compute_region_disk": resourceComputeRegionDisk(),
"google_compute_route": resourceComputeRoute(),
"google_compute_router": resourceComputeRouter(),
"google_compute_snapshot": resourceComputeSnapshot(),
"google_compute_ssl_certificate": resourceComputeSslCertificate(),
"google_compute_managed_ssl_certificate": resourceComputeManagedSslCertificate(),
"google_compute_ssl_policy": resourceComputeSslPolicy(),
"google_compute_subnetwork": resourceComputeSubnetwork(),
"google_compute_target_http_proxy": resourceComputeTargetHttpProxy(),
"google_compute_target_https_proxy": resourceComputeTargetHttpsProxy(),
"google_compute_target_ssl_proxy": resourceComputeTargetSslProxy(),
"google_compute_target_tcp_proxy": resourceComputeTargetTcpProxy(),
"google_compute_vpn_gateway": resourceComputeVpnGateway(),
"google_compute_url_map": resourceComputeUrlMap(),
"google_compute_vpn_tunnel": resourceComputeVpnTunnel(),
"google_compute_address": resourceComputeAddress(),
"google_compute_autoscaler": resourceComputeAutoscaler(),
"google_compute_backend_bucket": resourceComputeBackendBucket(),
"google_compute_backend_bucket_signed_url_key": resourceComputeBackendBucketSignedUrlKey(),
"google_compute_disk": resourceComputeDisk(),
"google_compute_firewall": resourceComputeFirewall(),
"google_compute_forwarding_rule": resourceComputeForwardingRule(),
"google_compute_global_address": resourceComputeGlobalAddress(),
"google_compute_http_health_check": resourceComputeHttpHealthCheck(),
"google_compute_https_health_check": resourceComputeHttpsHealthCheck(),
"google_compute_health_check": resourceComputeHealthCheck(),
"google_compute_image": resourceComputeImage(),
"google_compute_interconnect_attachment": resourceComputeInterconnectAttachment(),
"google_compute_network": resourceComputeNetwork(),
"google_compute_region_autoscaler": resourceComputeRegionAutoscaler(),
"google_compute_region_disk": resourceComputeRegionDisk(),
"google_compute_route": resourceComputeRoute(),
"google_compute_router": resourceComputeRouter(),
"google_compute_snapshot": resourceComputeSnapshot(),
"google_compute_ssl_certificate": resourceComputeSslCertificate(),
"google_compute_managed_ssl_certificate": resourceComputeManagedSslCertificate(),
"google_compute_ssl_policy": resourceComputeSslPolicy(),
"google_compute_subnetwork": resourceComputeSubnetwork(),
"google_compute_target_http_proxy": resourceComputeTargetHttpProxy(),
"google_compute_target_https_proxy": resourceComputeTargetHttpsProxy(),
"google_compute_target_ssl_proxy": resourceComputeTargetSslProxy(),
"google_compute_target_tcp_proxy": resourceComputeTargetTcpProxy(),
"google_compute_vpn_gateway": resourceComputeVpnGateway(),
"google_compute_url_map": resourceComputeUrlMap(),
"google_compute_vpn_tunnel": resourceComputeVpnTunnel(),
}
77 changes: 77 additions & 0 deletions google-beta/resource_compute_backend_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"
"log"
"reflect"
"strconv"
"time"

"github.com/hashicorp/terraform/helper/schema"
Expand Down Expand Up @@ -52,6 +53,21 @@ func resourceComputeBackendBucket() *schema.Resource {
ForceNew: true,
ValidateFunc: validateRegexp(`^(?:[a-z](?:[-a-z0-9]{0,61}[a-z0-9])?)$`),
},
"cdn_policy": {
Type: schema.TypeList,
Computed: true,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"signed_url_cache_max_age_sec": {
Type: schema.TypeInt,
Optional: true,
Default: 3600,
},
},
},
},
"description": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -88,6 +104,12 @@ func resourceComputeBackendBucketCreate(d *schema.ResourceData, meta interface{}
} else if v, ok := d.GetOkExists("bucket_name"); !isEmptyValue(reflect.ValueOf(bucketNameProp)) && (ok || !reflect.DeepEqual(v, bucketNameProp)) {
obj["bucketName"] = bucketNameProp
}
cdnPolicyProp, err := expandComputeBackendBucketCdnPolicy(d.Get("cdn_policy"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("cdn_policy"); !isEmptyValue(reflect.ValueOf(cdnPolicyProp)) && (ok || !reflect.DeepEqual(v, cdnPolicyProp)) {
obj["cdnPolicy"] = cdnPolicyProp
}
descriptionProp, err := expandComputeBackendBucketDescription(d.Get("description"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -174,6 +196,9 @@ func resourceComputeBackendBucketRead(d *schema.ResourceData, meta interface{})
if err := d.Set("bucket_name", flattenComputeBackendBucketBucketName(res["bucketName"], d)); err != nil {
return fmt.Errorf("Error reading BackendBucket: %s", err)
}
if err := d.Set("cdn_policy", flattenComputeBackendBucketCdnPolicy(res["cdnPolicy"], d)); err != nil {
return fmt.Errorf("Error reading BackendBucket: %s", err)
}
if err := d.Set("creation_timestamp", flattenComputeBackendBucketCreationTimestamp(res["creationTimestamp"], d)); err != nil {
return fmt.Errorf("Error reading BackendBucket: %s", err)
}
Expand Down Expand Up @@ -203,6 +228,12 @@ func resourceComputeBackendBucketUpdate(d *schema.ResourceData, meta interface{}
} else if v, ok := d.GetOkExists("bucket_name"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, bucketNameProp)) {
obj["bucketName"] = bucketNameProp
}
cdnPolicyProp, err := expandComputeBackendBucketCdnPolicy(d.Get("cdn_policy"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("cdn_policy"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, cdnPolicyProp)) {
obj["cdnPolicy"] = cdnPolicyProp
}
descriptionProp, err := expandComputeBackendBucketDescription(d.Get("description"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -312,6 +343,29 @@ func flattenComputeBackendBucketBucketName(v interface{}, d *schema.ResourceData
return v
}

func flattenComputeBackendBucketCdnPolicy(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["signed_url_cache_max_age_sec"] =
flattenComputeBackendBucketCdnPolicySignedUrlCacheMaxAgeSec(original["signedUrlCacheMaxAgeSec"], d)
return []interface{}{transformed}
}
func flattenComputeBackendBucketCdnPolicySignedUrlCacheMaxAgeSec(v interface{}, d *schema.ResourceData) interface{} {
// Handles the string fixed64 format
if strVal, ok := v.(string); ok {
if intVal, err := strconv.ParseInt(strVal, 10, 64); err == nil {
return intVal
} // let terraform core handle it if we can't convert the string to an int.
}
return v
}

func flattenComputeBackendBucketCreationTimestamp(v interface{}, d *schema.ResourceData) interface{} {
return v
}
Expand All @@ -332,6 +386,29 @@ func expandComputeBackendBucketBucketName(v interface{}, d TerraformResourceData
return v, nil
}

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

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

return transformed, nil
}

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

func expandComputeBackendBucketDescription(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
Expand Down
Loading

0 comments on commit f8c5a28

Please sign in to comment.