diff --git a/google/config.go b/google/config.go index bcec30dd20f..480a99d7bc5 100644 --- a/google/config.go +++ b/google/config.go @@ -92,6 +92,7 @@ type Config struct { PubsubBasePath string RedisBasePath string ResourceManagerBasePath string + RuntimeconfigBasePath string SecurityCenterBasePath string SourceRepoBasePath string SpannerBasePath string @@ -147,8 +148,7 @@ type Config struct { ResourceManagerV2Beta1BasePath string clientResourceManagerV2Beta1 *resourceManagerV2Beta1.Service - RuntimeconfigBasePath string - clientRuntimeconfig *runtimeconfig.Service + clientRuntimeconfig *runtimeconfig.Service clientSpanner *spanner.Service @@ -218,6 +218,7 @@ var MonitoringDefaultBasePath = "https://monitoring.googleapis.com/v3/" var PubsubDefaultBasePath = "https://pubsub.googleapis.com/v1/" var RedisDefaultBasePath = "https://redis.googleapis.com/v1/" var ResourceManagerDefaultBasePath = "https://cloudresourcemanager.googleapis.com/v1/" +var RuntimeconfigDefaultBasePath = "https://runtimeconfig.googleapis.com/v1beta1/" var SecurityCenterDefaultBasePath = "https://securitycenter.googleapis.com/v1/" var SourceRepoDefaultBasePath = "https://sourcerepo.googleapis.com/v1/" var SpannerDefaultBasePath = "https://spanner.googleapis.com/v1/" @@ -681,6 +682,7 @@ func ConfigureBasePaths(c *Config) { c.PubsubBasePath = PubsubDefaultBasePath c.RedisBasePath = RedisDefaultBasePath c.ResourceManagerBasePath = ResourceManagerDefaultBasePath + c.RuntimeconfigBasePath = RuntimeconfigDefaultBasePath c.SecurityCenterBasePath = SecurityCenterDefaultBasePath c.SourceRepoBasePath = SourceRepoDefaultBasePath c.SpannerBasePath = SpannerDefaultBasePath @@ -699,7 +701,6 @@ func ConfigureBasePaths(c *Config) { c.DnsBetaBasePath = DnsBetaDefaultBasePath c.IamCredentialsBasePath = IamCredentialsDefaultBasePath c.ResourceManagerV2Beta1BasePath = ResourceManagerV2Beta1DefaultBasePath - c.RuntimeconfigBasePath = RuntimeconfigDefaultBasePath c.IAMBasePath = IAMDefaultBasePath c.ServiceManagementBasePath = ServiceManagementDefaultBasePath c.ServiceNetworkingBasePath = ServiceNetworkingDefaultBasePath diff --git a/google/iam_runtimeconfig_config.go b/google/iam_runtimeconfig_config.go new file mode 100644 index 00000000000..48e4f31a2d6 --- /dev/null +++ b/google/iam_runtimeconfig_config.go @@ -0,0 +1,169 @@ +// ---------------------------------------------------------------------------- +// +// *** 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" + + "github.com/hashicorp/errwrap" + "github.com/hashicorp/terraform/helper/schema" + "google.golang.org/api/cloudresourcemanager/v1" +) + +var RuntimeconfigConfigIamSchema = map[string]*schema.Schema{ + "project": { + Type: schema.TypeString, + Computed: true, + Optional: true, + ForceNew: true, + }, + "config": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + DiffSuppressFunc: compareSelfLinkOrResourceName, + }, +} + +type RuntimeconfigConfigIamUpdater struct { + project string + config string + d *schema.ResourceData + Config *Config +} + +func RuntimeconfigConfigIamUpdaterProducer(d *schema.ResourceData, config *Config) (ResourceIamUpdater, error) { + values := make(map[string]string) + + project, err := getProject(d, config) + if err != nil { + return nil, err + } + values["project"] = project + + // We may have gotten either a long or short name, so attempt to parse long name if possible + m, err := getImportIdQualifiers([]string{"projects/(?P[^/]+)/configs/(?P[^/]+)", "(?P[^/]+)/(?P[^/]+)", "(?P[^/]+)"}, d, config, d.Get("config").(string)) + if err != nil { + return nil, err + } + + for k, v := range m { + values[k] = v + } + + u := &RuntimeconfigConfigIamUpdater{ + project: values["project"], + config: values["config"], + d: d, + Config: config, + } + + d.Set("project", u.project) + d.Set("config", u.GetResourceId()) + + d.SetId(u.GetResourceId()) + + return u, nil +} + +func RuntimeconfigConfigIdParseFunc(d *schema.ResourceData, config *Config) error { + values := make(map[string]string) + + project, err := getProject(d, config) + if err != nil { + return err + } + values["project"] = project + + m, err := getImportIdQualifiers([]string{"projects/(?P[^/]+)/configs/(?P[^/]+)", "(?P[^/]+)/(?P[^/]+)", "(?P[^/]+)"}, d, config, d.Id()) + if err != nil { + return err + } + + for k, v := range m { + values[k] = v + } + + u := &RuntimeconfigConfigIamUpdater{ + project: values["project"], + config: values["config"], + d: d, + Config: config, + } + d.Set("config", u.GetResourceId()) + d.SetId(u.GetResourceId()) + return nil +} + +func (u *RuntimeconfigConfigIamUpdater) GetResourceIamPolicy() (*cloudresourcemanager.Policy, error) { + url := u.qualifyConfigUrl("getIamPolicy") + + project, err := getProject(u.d, u.Config) + if err != nil { + return nil, err + } + + policy, err := sendRequest(u.Config, "GET", project, url, nil) + if err != nil { + return nil, errwrap.Wrapf(fmt.Sprintf("Error retrieving IAM policy for %s: {{err}}", u.DescribeResource()), err) + } + + out := &cloudresourcemanager.Policy{} + err = Convert(policy, out) + if err != nil { + return nil, errwrap.Wrapf("Cannot convert a policy to a resource manager policy: {{err}}", err) + } + + return out, nil +} + +func (u *RuntimeconfigConfigIamUpdater) SetResourceIamPolicy(policy *cloudresourcemanager.Policy) error { + json, err := ConvertToMap(policy) + if err != nil { + return err + } + + obj := make(map[string]interface{}) + obj["policy"] = json + + url := u.qualifyConfigUrl("setIamPolicy") + + project, err := getProject(u.d, u.Config) + if err != nil { + return err + } + + _, err = sendRequestWithTimeout(u.Config, "POST", project, url, obj, u.d.Timeout(schema.TimeoutCreate)) + if err != nil { + return errwrap.Wrapf(fmt.Sprintf("Error setting IAM policy for %s: {{err}}", u.DescribeResource()), err) + } + + return nil +} + +func (u *RuntimeconfigConfigIamUpdater) qualifyConfigUrl(methodIdentifier string) string { + return fmt.Sprintf("https://runtimeconfig.googleapis.com/v1beta1/%s:%s", fmt.Sprintf("projects/%s/configs/%s", u.project, u.config), methodIdentifier) +} + +func (u *RuntimeconfigConfigIamUpdater) GetResourceId() string { + return fmt.Sprintf("projects/%s/configs/%s", u.project, u.config) +} + +func (u *RuntimeconfigConfigIamUpdater) GetMutexKey() string { + return fmt.Sprintf("iam-runtimeconfig-config-%s", u.GetResourceId()) +} + +func (u *RuntimeconfigConfigIamUpdater) DescribeResource() string { + return fmt.Sprintf("runtimeconfig config %q", u.GetResourceId()) +} diff --git a/google/iam_runtimeconfig_config_generated_test.go b/google/iam_runtimeconfig_config_generated_test.go new file mode 100644 index 00000000000..5d320f744b2 --- /dev/null +++ b/google/iam_runtimeconfig_config_generated_test.go @@ -0,0 +1,179 @@ +// ---------------------------------------------------------------------------- +// +// *** 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" + "testing" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccRuntimeconfigConfigIamBindingGenerated(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "random_suffix": acctest.RandString(10), + "role": "roles/viewer", + } + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccRuntimeconfigConfigIamBinding_basicGenerated(context), + }, + { + ResourceName: "google_runtimeconfig_config_iam_binding.foo", + ImportStateId: fmt.Sprintf("projects/%s/configs/%s roles/viewer", getTestProjectFromEnv(), fmt.Sprintf("my-config%s", context["random_suffix"])), + ImportState: true, + ImportStateVerify: true, + }, + { + // Test Iam Binding update + Config: testAccRuntimeconfigConfigIamBinding_updateGenerated(context), + }, + { + ResourceName: "google_runtimeconfig_config_iam_binding.foo", + ImportStateId: fmt.Sprintf("projects/%s/configs/%s roles/viewer", getTestProjectFromEnv(), fmt.Sprintf("my-config%s", context["random_suffix"])), + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func TestAccRuntimeconfigConfigIamMemberGenerated(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "random_suffix": acctest.RandString(10), + "role": "roles/viewer", + } + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + // Test Iam Member creation (no update for member, no need to test) + Config: testAccRuntimeconfigConfigIamMember_basicGenerated(context), + }, + { + ResourceName: "google_runtimeconfig_config_iam_member.foo", + ImportStateId: fmt.Sprintf("projects/%s/configs/%s roles/viewer user:admin@hashicorptest.com", getTestProjectFromEnv(), fmt.Sprintf("my-config%s", context["random_suffix"])), + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func TestAccRuntimeconfigConfigIamPolicyGenerated(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "random_suffix": acctest.RandString(10), + "role": "roles/viewer", + } + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccRuntimeconfigConfigIamPolicy_basicGenerated(context), + }, + { + ResourceName: "google_runtimeconfig_config_iam_policy.foo", + ImportStateId: fmt.Sprintf("projects/%s/configs/%s", getTestProjectFromEnv(), fmt.Sprintf("my-config%s", context["random_suffix"])), + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func testAccRuntimeconfigConfigIamMember_basicGenerated(context map[string]interface{}) string { + return Nprintf(` +resource "google_runtimeconfig_config" "config" { + name = "my-config%{random_suffix}" + description = "Runtime configuration values for my service" +} + +resource "google_runtimeconfig_config_iam_member" "foo" { + project = "${google_runtimeconfig_config.config.project}" + config = "${google_runtimeconfig_config.config.name}" + role = "%{role}" + member = "user:admin@hashicorptest.com" +} +`, context) +} + +func testAccRuntimeconfigConfigIamPolicy_basicGenerated(context map[string]interface{}) string { + return Nprintf(` +resource "google_runtimeconfig_config" "config" { + name = "my-config%{random_suffix}" + description = "Runtime configuration values for my service" +} + +data "google_iam_policy" "foo" { + binding { + role = "%{role}" + members = ["user:admin@hashicorptest.com"] + } +} + +resource "google_runtimeconfig_config_iam_policy" "foo" { + project = "${google_runtimeconfig_config.config.project}" + config = "${google_runtimeconfig_config.config.name}" + policy_data = "${data.google_iam_policy.foo.policy_data}" +} +`, context) +} + +func testAccRuntimeconfigConfigIamBinding_basicGenerated(context map[string]interface{}) string { + return Nprintf(` +resource "google_runtimeconfig_config" "config" { + name = "my-config%{random_suffix}" + description = "Runtime configuration values for my service" +} + +resource "google_runtimeconfig_config_iam_binding" "foo" { + project = "${google_runtimeconfig_config.config.project}" + config = "${google_runtimeconfig_config.config.name}" + role = "%{role}" + members = ["user:admin@hashicorptest.com"] +} +`, context) +} + +func testAccRuntimeconfigConfigIamBinding_updateGenerated(context map[string]interface{}) string { + return Nprintf(` +resource "google_runtimeconfig_config" "config" { + name = "my-config%{random_suffix}" + description = "Runtime configuration values for my service" +} + +resource "google_runtimeconfig_config_iam_binding" "foo" { + project = "${google_runtimeconfig_config.config.project}" + config = "${google_runtimeconfig_config.config.name}" + role = "%{role}" + members = ["user:admin@hashicorptest.com", "user:paddy@hashicorp.com"] +} +`, context) +} diff --git a/google/provider.go b/google/provider.go index 6b3075cbbdf..cbc53cf687a 100644 --- a/google/provider.go +++ b/google/provider.go @@ -279,6 +279,14 @@ func Provider() terraform.ResourceProvider { "GOOGLE_RESOURCE_MANAGER_CUSTOM_ENDPOINT", }, ResourceManagerDefaultBasePath), }, + "runtimeconfig_custom_endpoint": { + Type: schema.TypeString, + Optional: true, + ValidateFunc: validateCustomEndpoint, + DefaultFunc: schema.MultiEnvDefaultFunc([]string{ + "GOOGLE_RUNTIMECONFIG_CUSTOM_ENDPOINT", + }, RuntimeconfigDefaultBasePath), + }, "security_center_custom_endpoint": { Type: schema.TypeString, Optional: true, @@ -421,9 +429,9 @@ func Provider() terraform.ResourceProvider { return provider } -// Generated resources: 80 -// Generated IAM resources: 21 -// Total generated resources: 101 +// Generated resources: 81 +// Generated IAM resources: 24 +// Total generated resources: 105 func ResourceMap() map[string]*schema.Resource { resourceMap, _ := ResourceMapWithErrors() return resourceMap @@ -517,6 +525,9 @@ func ResourceMapWithErrors() (map[string]*schema.Resource, error) { "google_pubsub_subscription": resourcePubsubSubscription(), "google_redis_instance": resourceRedisInstance(), "google_resource_manager_lien": resourceResourceManagerLien(), + "google_runtimeconfig_config_iam_binding": ResourceIamBinding(RuntimeconfigConfigIamSchema, RuntimeconfigConfigIamUpdaterProducer, RuntimeconfigConfigIdParseFunc), + "google_runtimeconfig_config_iam_member": ResourceIamMember(RuntimeconfigConfigIamSchema, RuntimeconfigConfigIamUpdaterProducer, RuntimeconfigConfigIdParseFunc), + "google_runtimeconfig_config_iam_policy": ResourceIamPolicy(RuntimeconfigConfigIamSchema, RuntimeconfigConfigIamUpdaterProducer, RuntimeconfigConfigIdParseFunc), "google_scc_source": resourceSecurityCenterSource(), "google_sourcerepo_repository": resourceSourceRepoRepository(), "google_sourcerepo_repository_iam_binding": ResourceIamBinding(SourceRepoRepositoryIamSchema, SourceRepoRepositoryIamUpdaterProducer, SourceRepoRepositoryIdParseFunc), @@ -702,6 +713,7 @@ func providerConfigure(d *schema.ResourceData, terraformVersion string) (interfa config.PubsubBasePath = d.Get("pubsub_custom_endpoint").(string) config.RedisBasePath = d.Get("redis_custom_endpoint").(string) config.ResourceManagerBasePath = d.Get("resource_manager_custom_endpoint").(string) + config.RuntimeconfigBasePath = d.Get("runtimeconfig_custom_endpoint").(string) config.SecurityCenterBasePath = d.Get("security_center_custom_endpoint").(string) config.SourceRepoBasePath = d.Get("source_repo_custom_endpoint").(string) config.SpannerBasePath = d.Get("spanner_custom_endpoint").(string) diff --git a/google/provider_handwritten_endpoint.go b/google/provider_handwritten_endpoint.go index 4fff62846d0..2137d5eb6f2 100644 --- a/google/provider_handwritten_endpoint.go +++ b/google/provider_handwritten_endpoint.go @@ -139,7 +139,6 @@ var ResourceManagerV2Beta1CustomEndpointEntry = &schema.Schema{ }, ResourceManagerV2Beta1DefaultBasePath), } -var RuntimeconfigDefaultBasePath = "https://runtimeconfig.googleapis.com/v1beta1/" var RuntimeconfigCustomEndpointEntryKey = "runtimeconfig_custom_endpoint" var RuntimeconfigCustomEndpointEntry = &schema.Schema{ Type: schema.TypeString, diff --git a/google/resource_service_networking_connection.go b/google/resource_service_networking_connection.go index b0342b62cb8..42c1f716ab1 100644 --- a/google/resource_service_networking_connection.go +++ b/google/resource_service_networking_connection.go @@ -177,11 +177,12 @@ func resourceServiceNetworkingConnectionDelete(d *schema.ResourceData, meta inte obj["name"] = peering url := fmt.Sprintf("%s%s/removePeering", config.ComputeBasePath, serviceNetworkingNetworkName) - project, err := getProject(d, config) + networkFieldValue, err := ParseNetworkFieldValue(network, d, config) if err != nil { - return err + return errwrap.Wrapf("Failed to retrieve network field value, err: {{err}}", err) } + project := networkFieldValue.Project res, err := sendRequestWithTimeout(config, "POST", project, url, obj, d.Timeout(schema.TimeoutUpdate)) if err != nil { return handleNotFoundError(err, d, fmt.Sprintf("ServiceNetworkingConnection %q", d.Id())) diff --git a/website/docs/r/runtimeconfig_config_iam.html.markdown b/website/docs/r/runtimeconfig_config_iam.html.markdown new file mode 100644 index 00000000000..52f281db13d --- /dev/null +++ b/website/docs/r/runtimeconfig_config_iam.html.markdown @@ -0,0 +1,127 @@ +--- +# ---------------------------------------------------------------------------- +# +# *** 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_runtimeconfig_config_iam" +sidebar_current: "docs-google-runtimeconfig-config-iam" +description: |- + Collection of resources to manage IAM policy for RuntimeconfigConfig +--- + +# IAM policy for RuntimeconfigConfig +Three different resources help you manage your IAM policy for Runtimeconfig Config. Each of these resources serves a different use case: + +* `google_runtimeconfig_config_iam_policy`: Authoritative. Sets the IAM policy for the config and replaces any existing policy already attached. +* `google_runtimeconfig_config_iam_binding`: Authoritative for a given role. Updates the IAM policy to grant a role to a list of members. Other roles within the IAM policy for the config are preserved. +* `google_runtimeconfig_config_iam_member`: Non-authoritative. Updates the IAM policy to grant a role to a new member. Other members for the role for the config are preserved. + +~> **Note:** `google_runtimeconfig_config_iam_policy` **cannot** be used in conjunction with `google_runtimeconfig_config_iam_binding` and `google_runtimeconfig_config_iam_member` or they will fight over what your policy should be. + +~> **Note:** `google_runtimeconfig_config_iam_binding` resources **can be** used in conjunction with `google_runtimeconfig_config_iam_member` resources **only if** they do not grant privilege to the same role. + + + +## google\_runtimeconfig\_config\_iam\_policy + +```hcl +data "google_iam_policy" "admin" { + binding { + role = "roles/viewer" + members = [ + "user:jane@example.com", + ] + } +} + +resource "google_runtimeconfig_config_iam_policy" "editor" { + project = "${google_runtimeconfig_config.config.project}" + config = "${google_runtimeconfig_config.config.name}" + policy_data = "${data.google_iam_policy.admin.policy_data}" +} +``` + +## google\_runtimeconfig\_config\_iam\_binding + +```hcl +resource "google_runtimeconfig_config_iam_binding" "editor" { + project = "${google_runtimeconfig_config.config.project}" + config = "${google_runtimeconfig_config.config.name}" + role = "roles/viewer" + members = [ + "user:jane@example.com", + ] +} +``` + +## google\_runtimeconfig\_config\_iam\_member + +```hcl +resource "google_runtimeconfig_config_iam_member" "editor" { + project = "${google_runtimeconfig_config.config.project}" + config = "${google_runtimeconfig_config.config.name}" + role = "roles/viewer" + member = "user:jane@example.com" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `config` - (Required) Used to find the parent resource to bind the IAM policy to + +* `project` - (Optional) The ID of the project in which the resource belongs. + If it is not provided, the project will be parsed from the identifier of the parent resource. If no project is provided in the parent identifier and no project is specified, the provider project is used. + +* `member/members` - (Required) Identities that will be granted the privilege in `role`. + Each entry can have one of the following values: + * **allUsers**: A special identifier that represents anyone who is on the internet; with or without a Google account. + * **allAuthenticatedUsers**: A special identifier that represents anyone who is authenticated with a Google account or a service account. + * **user:{emailid}**: An email address that represents a specific Google account. For example, alice@gmail.com or joe@example.com. + * **serviceAccount:{emailid}**: An email address that represents a service account. For example, my-other-app@appspot.gserviceaccount.com. + * **group:{emailid}**: An email address that represents a Google group. For example, admins@example.com. + * **domain:{domain}**: A G Suite domain (primary, instead of alias) name that represents all the users of that domain. For example, google.com or example.com. + +* `role` - (Required) The role that should be applied. Only one + `google_runtimeconfig_config_iam_binding` can be used per role. Note that custom roles must be of the format + `[projects|organizations]/{parent-name}/roles/{role-name}`. + +* `policy_data` - (Required only by `google_runtimeconfig_config_iam_policy`) The policy data generated by + a `google_iam_policy` data source. + +## Attributes Reference + +In addition to the arguments listed above, the following computed attributes are +exported: + +* `etag` - (Computed) The etag of the IAM policy. + +## Import + +Runtimeconfig config IAM resources can be imported using the project, resource identifiers, role and member. + +``` +$ terraform import google_runtimeconfig_config_iam_policy.editor projects/{{project}}/configs/{{config}} + +$ terraform import google_runtimeconfig_config_iam_binding.editor "projects/{{project}}/configs/{{config}} roles/viewer" + +$ terraform import google_runtimeconfig_config_iam_member.editor "projects/{{project}}/configs/{{config}} roles/viewer jane@example.com" +``` + +-> 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).