diff --git a/google/provider.go b/google/provider.go index 48cfbce13cc..cdea8a92069 100644 --- a/google/provider.go +++ b/google/provider.go @@ -437,9 +437,9 @@ func Provider() terraform.ResourceProvider { return provider } -// Generated resources: 80 +// Generated resources: 81 // Generated IAM resources: 33 -// Total generated resources: 113 +// Total generated resources: 114 func ResourceMap() map[string]*schema.Resource { resourceMap, _ := ResourceMapWithErrors() return resourceMap @@ -454,6 +454,7 @@ func ResourceMapWithErrors() (map[string]*schema.Resource, error) { "google_app_engine_domain_mapping": resourceAppEngineDomainMapping(), "google_app_engine_firewall_rule": resourceAppEngineFirewallRule(), "google_app_engine_standard_app_version": resourceAppEngineStandardAppVersion(), + "google_app_engine_application_url_dispatch_rules": resourceAppEngineApplicationUrlDispatchRules(), "google_bigquery_dataset": resourceBigQueryDataset(), "google_bigquery_data_transfer_config": resourceBigqueryDataTransferConfig(), "google_bigtable_app_profile": resourceBigtableAppProfile(), diff --git a/google/resource_app_engine_application_url_dispatch_rules.go b/google/resource_app_engine_application_url_dispatch_rules.go new file mode 100644 index 00000000000..da0943a6086 --- /dev/null +++ b/google/resource_app_engine_application_url_dispatch_rules.go @@ -0,0 +1,359 @@ +// ---------------------------------------------------------------------------- +// +// *** AUTO GENERATED CODE *** AUTO GENERATED CODE *** +// +// ---------------------------------------------------------------------------- +// +// This file is automatically generated by Magic Modules and manual +// changes will be clobbered when the file is regenerated. +// +// Please read more about how to change this file in +// .github/CONTRIBUTING.md. +// +// ---------------------------------------------------------------------------- + +package google + +import ( + "fmt" + "log" + "reflect" + "time" + + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + "google.golang.org/api/appengine/v1" +) + +func resourceAppEngineApplicationUrlDispatchRules() *schema.Resource { + return &schema.Resource{ + Create: resourceAppEngineApplicationUrlDispatchRulesCreate, + Read: resourceAppEngineApplicationUrlDispatchRulesRead, + Update: resourceAppEngineApplicationUrlDispatchRulesUpdate, + Delete: resourceAppEngineApplicationUrlDispatchRulesDelete, + + Importer: &schema.ResourceImporter{ + State: resourceAppEngineApplicationUrlDispatchRulesImport, + }, + + Timeouts: &schema.ResourceTimeout{ + Create: schema.DefaultTimeout(4 * time.Minute), + Update: schema.DefaultTimeout(4 * time.Minute), + Delete: schema.DefaultTimeout(4 * time.Minute), + }, + + Schema: map[string]*schema.Schema{ + "dispatch_rules": { + Type: schema.TypeList, + Required: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "path": { + Type: schema.TypeString, + Required: true, + }, + "service": { + Type: schema.TypeString, + Required: true, + }, + "domain": { + Type: schema.TypeString, + Optional: true, + Default: "*", + }, + }, + }, + }, + "project": { + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + }, + }, + } +} + +func resourceAppEngineApplicationUrlDispatchRulesCreate(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + + obj := make(map[string]interface{}) + dispatchRulesProp, err := expandAppEngineApplicationUrlDispatchRulesDispatchRules(d.Get("dispatch_rules"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("dispatch_rules"); !isEmptyValue(reflect.ValueOf(dispatchRulesProp)) && (ok || !reflect.DeepEqual(v, dispatchRulesProp)) { + obj["dispatchRules"] = dispatchRulesProp + } + + lockName, err := replaceVars(d, config, "apps/{{project}}") + if err != nil { + return err + } + mutexKV.Lock(lockName) + defer mutexKV.Unlock(lockName) + + url, err := replaceVars(d, config, "{{AppEngineBasePath}}apps/{{project}}?updateMask=dispatch_rules") + if err != nil { + return err + } + + log.Printf("[DEBUG] Creating new ApplicationUrlDispatchRules: %#v", obj) + project, err := getProject(d, config) + if err != nil { + return err + } + res, err := sendRequestWithTimeout(config, "PATCH", project, url, obj, d.Timeout(schema.TimeoutCreate)) + if err != nil { + return fmt.Errorf("Error creating ApplicationUrlDispatchRules: %s", err) + } + + // Store the ID now + id, err := replaceVars(d, config, "{{project}}") + if err != nil { + return fmt.Errorf("Error constructing id: %s", err) + } + d.SetId(id) + + op := &appengine.Operation{} + err = Convert(res, op) + if err != nil { + return err + } + + waitErr := appEngineOperationWaitTime( + config.clientAppEngine, op, project, "Creating ApplicationUrlDispatchRules", + int(d.Timeout(schema.TimeoutCreate).Minutes())) + + if waitErr != nil { + // The resource didn't actually create + d.SetId("") + return fmt.Errorf("Error waiting to create ApplicationUrlDispatchRules: %s", waitErr) + } + + log.Printf("[DEBUG] Finished creating ApplicationUrlDispatchRules %q: %#v", d.Id(), res) + + return resourceAppEngineApplicationUrlDispatchRulesRead(d, meta) +} + +func resourceAppEngineApplicationUrlDispatchRulesRead(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + + url, err := replaceVars(d, config, "{{AppEngineBasePath}}apps/{{project}}/{{name}}") + if err != nil { + return err + } + + project, err := getProject(d, config) + if err != nil { + return err + } + res, err := sendRequest(config, "GET", project, url, nil) + if err != nil { + return handleNotFoundError(err, d, fmt.Sprintf("AppEngineApplicationUrlDispatchRules %q", d.Id())) + } + + if err := d.Set("project", project); err != nil { + return fmt.Errorf("Error reading ApplicationUrlDispatchRules: %s", err) + } + + if err := d.Set("dispatch_rules", flattenAppEngineApplicationUrlDispatchRulesDispatchRules(res["dispatchRules"], d)); err != nil { + return fmt.Errorf("Error reading ApplicationUrlDispatchRules: %s", err) + } + + return nil +} + +func resourceAppEngineApplicationUrlDispatchRulesUpdate(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + + project, err := getProject(d, config) + if err != nil { + return err + } + + obj := make(map[string]interface{}) + dispatchRulesProp, err := expandAppEngineApplicationUrlDispatchRulesDispatchRules(d.Get("dispatch_rules"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("dispatch_rules"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, dispatchRulesProp)) { + obj["dispatchRules"] = dispatchRulesProp + } + + lockName, err := replaceVars(d, config, "apps/{{project}}") + if err != nil { + return err + } + mutexKV.Lock(lockName) + defer mutexKV.Unlock(lockName) + + url, err := replaceVars(d, config, "{{AppEngineBasePath}}apps/{{project}}?updateMask=dispatch_rules") + if err != nil { + return err + } + + log.Printf("[DEBUG] Updating ApplicationUrlDispatchRules %q: %#v", d.Id(), obj) + res, err := sendRequestWithTimeout(config, "PATCH", project, url, obj, d.Timeout(schema.TimeoutUpdate)) + + if err != nil { + return fmt.Errorf("Error updating ApplicationUrlDispatchRules %q: %s", d.Id(), err) + } + + op := &appengine.Operation{} + err = Convert(res, op) + if err != nil { + return err + } + + err = appEngineOperationWaitTime( + config.clientAppEngine, op, project, "Updating ApplicationUrlDispatchRules", + int(d.Timeout(schema.TimeoutUpdate).Minutes())) + + if err != nil { + return err + } + + return resourceAppEngineApplicationUrlDispatchRulesRead(d, meta) +} + +func resourceAppEngineApplicationUrlDispatchRulesDelete(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + + project, err := getProject(d, config) + if err != nil { + return err + } + + lockName, err := replaceVars(d, config, "apps/{{project}}") + if err != nil { + return err + } + mutexKV.Lock(lockName) + defer mutexKV.Unlock(lockName) + + url, err := replaceVars(d, config, "{{AppEngineBasePath}}apps/{{project}}?updateMask=dispatch_rules") + if err != nil { + return err + } + + var obj map[string]interface{} + log.Printf("[DEBUG] Deleting ApplicationUrlDispatchRules %q", d.Id()) + + res, err := sendRequestWithTimeout(config, "PATCH", project, url, obj, d.Timeout(schema.TimeoutDelete)) + if err != nil { + return handleNotFoundError(err, d, "ApplicationUrlDispatchRules") + } + + op := &appengine.Operation{} + err = Convert(res, op) + if err != nil { + return err + } + + err = appEngineOperationWaitTime( + config.clientAppEngine, op, project, "Deleting ApplicationUrlDispatchRules", + int(d.Timeout(schema.TimeoutDelete).Minutes())) + + if err != nil { + return err + } + + log.Printf("[DEBUG] Finished deleting ApplicationUrlDispatchRules %q: %#v", d.Id(), res) + return nil +} + +func resourceAppEngineApplicationUrlDispatchRulesImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + config := meta.(*Config) + if err := parseImportId([]string{ + "(?P[^/]+)", + }, d, config); err != nil { + return nil, err + } + + // Replace import id for the resource id + id, err := replaceVars(d, config, "{{project}}") + if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) + } + d.SetId(id) + + return []*schema.ResourceData{d}, nil +} + +func flattenAppEngineApplicationUrlDispatchRulesDispatchRules(v interface{}, d *schema.ResourceData) interface{} { + if v == nil { + return v + } + l := v.([]interface{}) + transformed := make([]interface{}, 0, len(l)) + for _, raw := range l { + original := raw.(map[string]interface{}) + if len(original) < 1 { + // Do not include empty json objects coming back from the api + continue + } + transformed = append(transformed, map[string]interface{}{ + "domain": flattenAppEngineApplicationUrlDispatchRulesDispatchRulesDomain(original["domain"], d), + "path": flattenAppEngineApplicationUrlDispatchRulesDispatchRulesPath(original["path"], d), + "service": flattenAppEngineApplicationUrlDispatchRulesDispatchRulesService(original["service"], d), + }) + } + return transformed +} +func flattenAppEngineApplicationUrlDispatchRulesDispatchRulesDomain(v interface{}, d *schema.ResourceData) interface{} { + return v +} + +func flattenAppEngineApplicationUrlDispatchRulesDispatchRulesPath(v interface{}, d *schema.ResourceData) interface{} { + return v +} + +func flattenAppEngineApplicationUrlDispatchRulesDispatchRulesService(v interface{}, d *schema.ResourceData) interface{} { + return v +} + +func expandAppEngineApplicationUrlDispatchRulesDispatchRules(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { + l := v.([]interface{}) + req := make([]interface{}, 0, len(l)) + for _, raw := range l { + if raw == nil { + continue + } + original := raw.(map[string]interface{}) + transformed := make(map[string]interface{}) + + transformedDomain, err := expandAppEngineApplicationUrlDispatchRulesDispatchRulesDomain(original["domain"], d, config) + if err != nil { + return nil, err + } else if val := reflect.ValueOf(transformedDomain); val.IsValid() && !isEmptyValue(val) { + transformed["domain"] = transformedDomain + } + + transformedPath, err := expandAppEngineApplicationUrlDispatchRulesDispatchRulesPath(original["path"], d, config) + if err != nil { + return nil, err + } else if val := reflect.ValueOf(transformedPath); val.IsValid() && !isEmptyValue(val) { + transformed["path"] = transformedPath + } + + transformedService, err := expandAppEngineApplicationUrlDispatchRulesDispatchRulesService(original["service"], d, config) + if err != nil { + return nil, err + } else if val := reflect.ValueOf(transformedService); val.IsValid() && !isEmptyValue(val) { + transformed["service"] = transformedService + } + + req = append(req, transformed) + } + return req, nil +} + +func expandAppEngineApplicationUrlDispatchRulesDispatchRulesDomain(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { + return v, nil +} + +func expandAppEngineApplicationUrlDispatchRulesDispatchRulesPath(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { + return v, nil +} + +func expandAppEngineApplicationUrlDispatchRulesDispatchRulesService(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { + return v, nil +} diff --git a/google/resource_app_engine_application_url_dispatch_rules_generated_test.go b/google/resource_app_engine_application_url_dispatch_rules_generated_test.go new file mode 100644 index 00000000000..9865cd86493 --- /dev/null +++ b/google/resource_app_engine_application_url_dispatch_rules_generated_test.go @@ -0,0 +1,112 @@ +// ---------------------------------------------------------------------------- +// +// *** AUTO GENERATED CODE *** AUTO GENERATED CODE *** +// +// ---------------------------------------------------------------------------- +// +// This file is automatically generated by Magic Modules and manual +// changes will be clobbered when the file is regenerated. +// +// Please read more about how to change this file in +// .github/CONTRIBUTING.md. +// +// ---------------------------------------------------------------------------- + +package google + +import ( + "log" + "strings" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/terraform" +) + +func TestAccAppEngineApplicationUrlDispatchRules_appEngineApplicationUrlDispatchRulesBasicExample(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "random_suffix": acctest.RandString(10), + } + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAppEngineApplicationUrlDispatchRulesDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAppEngineApplicationUrlDispatchRules_appEngineApplicationUrlDispatchRulesBasicExample(context), + }, + { + ResourceName: "google_app_engine_application_url_dispatch_rules.service_rules", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func testAccAppEngineApplicationUrlDispatchRules_appEngineApplicationUrlDispatchRulesBasicExample(context map[string]interface{}) string { + return Nprintf(` +resource "google_storage_bucket" "bucket" { + name = "appengine-test-bucket%{random_suffix}" +} + +resource "google_storage_bucket_object" "object" { + name = "hello-world.zip" + bucket = "${google_storage_bucket.bucket.name}" + source = "./test-fixtures/appengine/hello-world.zip" +} + +resource "google_app_engine_standard_app_version" "myapp_v1" { + version_id = "v1" + service = "myapp" + runtime = "nodejs10" + noop_on_destroy = true + entrypoint { + shell = "node ./app.js" + } + deployment { + zip { + source_url = "https://storage.googleapis.com/${google_storage_bucket.bucket.name}/hello-world.zip" + } + } + env_variables = { + port = "8080" + } + depends_on = ["google_storage_bucket_object.object"] + +} + +resource "google_app_engine_application_url_dispatch_rules" "service_rules" { + # project = "my-project" + dispatch_rules { + domain = "*" + path = "/default/*" + service = "default" + } + dispatch_rules { + domain = "*" + path = "/myapp/*" + service = "${google_app_engine_standard_app_version.myapp_v1.service}" + } +} +`, context) +} + +func testAccCheckAppEngineApplicationUrlDispatchRulesDestroy(s *terraform.State) error { + for name, rs := range s.RootModule().Resources { + if rs.Type != "google_app_engine_application_url_dispatch_rules" { + continue + } + if strings.HasPrefix(name, "data.") { + continue + } + + log.Printf("[DEBUG] Ignoring destroy during test") + } + + return nil +} diff --git a/google/resource_app_engine_standard_app_version.go b/google/resource_app_engine_standard_app_version.go index a8789490e93..4ae86d12d61 100644 --- a/google/resource_app_engine_standard_app_version.go +++ b/google/resource_app_engine_standard_app_version.go @@ -321,7 +321,7 @@ func resourceAppEngineStandardAppVersionCreate(d *schema.ResourceData, meta inte obj["instanceClass"] = instanceClassProp } - lockName, err := replaceVars(d, config, "apps/{{project}}/services/{{service}}") + lockName, err := replaceVars(d, config, "apps/{{project}}") if err != nil { return err } @@ -492,7 +492,7 @@ func resourceAppEngineStandardAppVersionUpdate(d *schema.ResourceData, meta inte obj["instanceClass"] = instanceClassProp } - lockName, err := replaceVars(d, config, "apps/{{project}}/services/{{service}}") + lockName, err := replaceVars(d, config, "apps/{{project}}") if err != nil { return err } diff --git a/website/docs/r/app_engine_application_url_dispatch_rules.html.markdown b/website/docs/r/app_engine_application_url_dispatch_rules.html.markdown new file mode 100644 index 00000000000..5643e149129 --- /dev/null +++ b/website/docs/r/app_engine_application_url_dispatch_rules.html.markdown @@ -0,0 +1,142 @@ +--- +# ---------------------------------------------------------------------------- +# +# *** AUTO GENERATED CODE *** AUTO GENERATED CODE *** +# +# ---------------------------------------------------------------------------- +# +# This file is automatically generated by Magic Modules and manual +# changes will be clobbered when the file is regenerated. +# +# Please read more about how to change this file in +# .github/CONTRIBUTING.md. +# +# ---------------------------------------------------------------------------- +layout: "google" +page_title: "Google: google_app_engine_application_url_dispatch_rules" +sidebar_current: "docs-google-app-engine-application-url-dispatch-rules" +description: |- + Rules to match an HTTP request and dispatch that request to a service. +--- + +# google\_app\_engine\_application\_url\_dispatch\_rules + +Rules to match an HTTP request and dispatch that request to a service. + + +To get more information about ApplicationUrlDispatchRules, see: + +* [API documentation](https://cloud.google.com/appengine/docs/admin-api/reference/rest/v1/apps#UrlDispatchRule) + +
+ + Open in Cloud Shell + +
+## Example Usage - App Engine Application Url Dispatch Rules Basic + + +```hcl +resource "google_storage_bucket" "bucket" { + name = "appengine-test-bucket" +} + +resource "google_storage_bucket_object" "object" { + name = "hello-world.zip" + bucket = "${google_storage_bucket.bucket.name}" + source = "./test-fixtures/appengine/hello-world.zip" +} + +resource "google_app_engine_standard_app_version" "myapp_v1" { + version_id = "v1" + service = "myapp" + runtime = "nodejs10" + noop_on_destroy = true + entrypoint { + shell = "node ./app.js" + } + deployment { + zip { + source_url = "https://storage.googleapis.com/${google_storage_bucket.bucket.name}/hello-world.zip" + } + } + env_variables = { + port = "8080" + } + depends_on = ["google_storage_bucket_object.object"] + +} + +resource "google_app_engine_application_url_dispatch_rules" "service_rules" { + # project = "my-project" + dispatch_rules { + domain = "*" + path = "/default/*" + service = "default" + } + dispatch_rules { + domain = "*" + path = "/myapp/*" + service = "${google_app_engine_standard_app_version.myapp_v1.service}" + } +} +``` + +## Argument Reference + +The following arguments are supported: + + +* `dispatch_rules` - + (Required) + Rules to match an HTTP request and dispatch that request to a service. Structure is documented below. + + +The `dispatch_rules` block supports: + +* `domain` - + (Optional) + Domain name to match against. The wildcard "*" is supported if specified before a period: "*.". + Defaults to matching all domains: "*". + +* `path` - + (Required) + Pathname within the host. Must start with a "/". A single "*" can be included at the end of the path. + The sum of the lengths of the domain and path may not exceed 100 characters. + +* `service` - + (Required) + Pathname within the host. Must start with a "/". A single "*" can be included at the end of the path. + The sum of the lengths of the domain and path may not exceed 100 characters. + +- - - + + +* `project` - (Optional) The ID of the project in which the resource belongs. + If it is not provided, the provider project is used. + + + +## Timeouts + +This resource provides the following +[Timeouts](/docs/configuration/resources.html#timeouts) configuration options: + +- `create` - Default is 4 minutes. +- `update` - Default is 4 minutes. +- `delete` - Default is 4 minutes. + +## Import + +ApplicationUrlDispatchRules can be imported using any of these accepted formats: + +``` +$ terraform import google_app_engine_application_url_dispatch_rules.default {{project}} +``` + +-> If you're importing a resource with beta features, make sure to include `-provider=google-beta` +as an argument so that Terraform uses the correct provider to import your resource. + +## User Project Overrides + +This resource supports [User Project Overrides](https://www.terraform.io/docs/providers/google/provider_reference.html#user_project_override). diff --git a/website/google.erb b/website/google.erb index e09f0447ff5..d4faaa2b12d 100644 --- a/website/google.erb +++ b/website/google.erb @@ -211,6 +211,9 @@ > google_app_engine_standard_app_version + > + google_app_engine_application_url_dispatch_rules +