diff --git a/products/accesscontextmanager/api.yaml b/products/accesscontextmanager/api.yaml index a9ba4660e640..a8fcbf729c41 100644 --- a/products/accesscontextmanager/api.yaml +++ b/products/accesscontextmanager/api.yaml @@ -84,6 +84,7 @@ objects: Time the AccessPolicy was updated in UTC. output: true - !ruby/object:Api::Resource + # This is the singular of `AccessLevels`, any changes here should be made to `AccessLevels` as well name: 'AccessLevel' # This is an unusual API, so we need to use a few fields to map the methods # to the right URL. @@ -326,6 +327,252 @@ objects: name: 'location' description: String indicating the location of the expression for error reporting, e.g. a file name and a position in the file - !ruby/object:Api::Resource + # This is the plural of `AccessLevel`, any changes here should be made to `AccessLevel` as well + name: 'AccessLevels' + base_url: "{{parent}}/accessLevels:replaceAll" + update_url: "{{parent}}/accessLevels:replaceAll" + self_link: "{{parent}}/accessLevels" + update_verb: :POST + references: !ruby/object:Api::Resource::ReferenceLinks + guides: + 'Access Policy Quickstart': 'https://cloud.google.com/access-context-manager/docs/quickstart' + api: 'https://cloud.google.com/access-context-manager/docs/reference/rest/v1/accessPolicies.accessLevels' + description: | + Replace all existing Access Levels in an Access Policy with the Access Levels provided. This is done atomically. + This is a bulk edit of all Access Levels and may override existing Access Levels created by `google_access_context_manager_access_level`, + thus causing a permadiff if used alongside `google_access_context_manager_access_level` on the same parent. + parameters: + # Parent is a path parameter that _cannot_ be read or sent in the request at all. + # This must be done at the provider level. + - !ruby/object:Api::Type::String + name: parent + input: true + required: true + url_param_only: true + description: | + The AccessPolicy this AccessLevel lives in. + Format: accessPolicies/{policy_id} + properties: + - !ruby/object:Api::Type::Array + name: 'accessLevels' + description: | + The desired Access Levels that should replace all existing Access Levels in the Access Policy. + item_type: !ruby/object:Api::Type::NestedObject + properties: + - !ruby/object:Api::Type::String + name: name + input: true + required: true + description: | + Resource name for the Access Level. The short_name component must begin + with a letter and only include alphanumeric and '_'. + Format: accessPolicies/{policy_id}/accessLevels/{short_name} + - !ruby/object:Api::Type::String + name: title + required: true + description: | + Human readable title. Must be unique within the Policy. + - !ruby/object:Api::Type::String + name: 'description' + description: | + Description of the AccessLevel and its use. Does not affect behavior. + # createTime and updateTime are listed in the docs but are not ever present + # in actual API responses. + - !ruby/object:Api::Type::Time + exclude: true + name: 'createTime' + description: | + Time the AccessPolicy was created in UTC. + output: true + - !ruby/object:Api::Type::Time + exclude: true + name: 'updateTime' + description: | + Time the AccessPolicy was updated in UTC. + output: true + - !ruby/object:Api::Type::NestedObject + name: 'basic' + description: | + A set of predefined conditions for the access level and a combining function. + # conflicts: + # - custom + properties: + - !ruby/object:Api::Type::Enum + name: 'combiningFunction' + description: | + How the conditions list should be combined to determine if a request + is granted this AccessLevel. If AND is used, each Condition in + conditions must be satisfied for the AccessLevel to be applied. If + OR is used, at least one Condition in conditions must be satisfied + for the AccessLevel to be applied. + default_value: :AND + values: + - :AND + - :OR + # All of the false defaults below here are omitted on purpose. + # Let's keep this as simple as possible, since they will all be set by the API. + - !ruby/object:Api::Type::Array + name: 'conditions' + required: true + min_size: 1 + description: | + A set of requirements for the AccessLevel to be granted. + item_type: !ruby/object:Api::Type::NestedObject + properties: + - !ruby/object:Api::Type::Array + name: 'ipSubnetworks' + description: | + A list of CIDR block IP subnetwork specification. May be IPv4 + or IPv6. + Note that for a CIDR IP address block, the specified IP address + portion must be properly truncated (i.e. all the host bits must + be zero) or the input is considered malformed. For example, + "192.0.2.0/24" is accepted but "192.0.2.1/24" is not. Similarly, + for IPv6, "2001:db8::/32" is accepted whereas "2001:db8::1/32" + is not. The originating IP of a request must be in one of the + listed subnets in order for this Condition to be true. + If empty, all IP addresses are allowed. + item_type: Api::Type::String + - !ruby/object:Api::Type::Array + name: 'requiredAccessLevels' + description: | + A list of other access levels defined in the same Policy, + referenced by resource name. Referencing an AccessLevel which + does not exist is an error. All access levels listed must be + granted for the Condition to be true. + Format: accessPolicies/{policy_id}/accessLevels/{short_name} + item_type: Api::Type::String + - !ruby/object:Api::Type::Array + name: 'members' + description: | + An allowed list of members (users, service accounts). + Using groups is not supported yet. + + The signed-in user originating the request must be a part of one + of the provided members. If not specified, a request may come + from any user (logged in/not logged in, not present in any + groups, etc.). + Formats: `user:{emailid}`, `serviceAccount:{emailid}` + item_type: Api::Type::String + - !ruby/object:Api::Type::Boolean + name: 'negate' + description: | + Whether to negate the Condition. If true, the Condition becomes + a NAND over its non-empty fields, each field must be false for + the Condition overall to be satisfied. Defaults to false. + - !ruby/object:Api::Type::NestedObject + name: 'devicePolicy' + description: | + Device specific restrictions, all restrictions must hold for + the Condition to be true. If not specified, all devices are + allowed. + properties: + - !ruby/object:Api::Type::Boolean + name: 'requireScreenLock' + api_name: 'requireScreenlock' + description: | + Whether or not screenlock is required for the DevicePolicy + to be true. Defaults to false. + - !ruby/object:Api::Type::Array + name: 'allowedEncryptionStatuses' + description: | + A list of allowed encryptions statuses. + An empty list allows all statuses. + item_type: !ruby/object:Api::Type::Enum + name: 'undefined' + description: | + This field only has a name and description because of MM + limitations. It should not appear in downstreams. + values: + - :ENCRYPTION_UNSPECIFIED + - :ENCRYPTION_UNSUPPORTED + - :UNENCRYPTED + - :ENCRYPTED + - !ruby/object:Api::Type::Array + name: 'allowedDeviceManagementLevels' + description: | + A list of allowed device management levels. + An empty list allows all management levels. + item_type: !ruby/object:Api::Type::Enum + name: 'undefined' + description: | + This field only has a name and description because of MM + limitations. It should not appear in downstreams. + values: + - :MANAGEMENT_UNSPECIFIED + - :NONE + - :BASIC + - :COMPLETE + - !ruby/object:Api::Type::Array + name: 'osConstraints' + description: | + A list of allowed OS versions. + An empty list allows all types and all versions. + item_type: !ruby/object:Api::Type::NestedObject + properties: + - !ruby/object:Api::Type::String + name: 'minimumVersion' + description: | + The minimum allowed OS version. If not set, any version + of this OS satisfies the constraint. + Format: "major.minor.patch" such as "10.5.301", "9.2.1". + - !ruby/object:Api::Type::Enum + name: 'osType' + required: true + description: | + The operating system type of the device. + values: + - :OS_UNSPECIFIED + - :DESKTOP_MAC + - :DESKTOP_WINDOWS + - :DESKTOP_LINUX + - :DESKTOP_CHROME_OS + - !ruby/object:Api::Type::Boolean + name: 'requireAdminApproval' + description: | + Whether the device needs to be approved by the customer admin. + - !ruby/object:Api::Type::Boolean + name: 'requireCorpOwned' + description: | + Whether the device needs to be corp owned. + - !ruby/object:Api::Type::Array + name: 'regions' + description: | + The request must originate from one of the provided + countries/regions. + Format: A valid ISO 3166-1 alpha-2 code. + item_type: Api::Type::String + - !ruby/object:Api::Type::NestedObject + name: 'custom' + description: | + Custom access level conditions are set using the Cloud Common Expression Language to represent the necessary conditions for the level to apply to a request. + See CEL spec at: https://github.com/google/cel-spec. + # conflicts: + # - basic + properties: + - !ruby/object:Api::Type::NestedObject + name: 'expr' + required: true + description: | + Represents a textual expression in the Common Expression Language (CEL) syntax. CEL is a C-like expression language. + This page details the objects and attributes that are used to the build the CEL expressions for + custom access levels - https://cloud.google.com/access-context-manager/docs/custom-access-level-spec. + properties: + - !ruby/object:Api::Type::String + name: 'expression' + required: true + description: Textual representation of an expression in Common Expression Language syntax. + - !ruby/object:Api::Type::String + name: 'title' + description: Title for the expression, i.e. a short string describing its purpose. + - !ruby/object:Api::Type::String + name: 'description' + description: Description of the expression + - !ruby/object:Api::Type::String + name: 'location' + description: String indicating the location of the expression for error reporting, e.g. a file name and a position in the file + - !ruby/object:Api::Resource + # This is the singular of `ServicePerimeters`, any changes here should be made to `ServicePerimeters` as well name: 'ServicePerimeter' # This is an unusual API, so we need to use a few fields to map the methods # to the right URL. @@ -558,6 +805,246 @@ objects: actually enforcing them. This testing is done through analyzing the differences between currently enforced and suggested restrictions. useExplicitDryRunSpec must bet set to True if any of the fields in the spec are set to non-default values. + - !ruby/object:Api::Resource + # This is the plural of `ServicePerimeter`, any changes here should be made to `ServicePerimeter` as well + name: 'ServicePerimeters' + base_url: "{{parent}}/servicePerimeters:replaceAll" + update_url: "{{parent}}/servicePerimeters:replaceAll" + self_link: "{{parent}}/servicePerimeters" + update_verb: :POST + references: !ruby/object:Api::Resource::ReferenceLinks + guides: + 'Service Perimeter Quickstart': 'https://cloud.google.com/vpc-service-controls/docs/quickstart' + api: 'https://cloud.google.com/access-context-manager/docs/reference/rest/v1/accessPolicies.servicePerimeters' + description: | + Replace all existing Service Perimeters in an Access Policy with the Service Perimeters provided. This is done atomically. + This is a bulk edit of all Service Perimeters and may override existing Service Perimeters created by `google_access_context_manager_service_perimeter`, + thus causing a permadiff if used alongside `google_access_context_manager_service_perimeter` on the same parent. + parameters: + # Parent is a path parameter that _cannot_ be read or sent in the request at all. + # This must be done at the provider level. + - !ruby/object:Api::Type::String + name: parent + input: true + required: true + description: | + The AccessPolicy this ServicePerimeter lives in. + Format: accessPolicies/{policy_id} + properties: + - !ruby/object:Api::Type::Array + name: 'servicePerimeters' + description: | + The desired Service Perimeters that should replace all existing Service Perimeters in the Access Policy. + item_type: !ruby/object:Api::Type::NestedObject + properties: + - !ruby/object:Api::Type::String + name: name + input: true + required: true + description: | + Resource name for the ServicePerimeter. The short_name component must + begin with a letter and only include alphanumeric and '_'. + Format: accessPolicies/{policy_id}/servicePerimeters/{short_name} + - !ruby/object:Api::Type::String + name: title + required: true + description: | + Human readable title. Must be unique within the Policy. + - !ruby/object:Api::Type::String + name: 'description' + description: | + Description of the ServicePerimeter and its use. Does not affect + behavior. + - !ruby/object:Api::Type::Time + name: 'createTime' + description: | + Time the AccessPolicy was created in UTC. + output: true + - !ruby/object:Api::Type::Time + name: 'updateTime' + description: | + Time the AccessPolicy was updated in UTC. + output: true + - !ruby/object:Api::Type::Enum + name: 'perimeterType' + description: | + Specifies the type of the Perimeter. There are two types: regular and + bridge. Regular Service Perimeter contains resources, access levels, + and restricted services. Every resource can be in at most + ONE regular Service Perimeter. + + In addition to being in a regular service perimeter, a resource can also + be in zero or more perimeter bridges. A perimeter bridge only contains + resources. Cross project operations are permitted if all effected + resources share some perimeter (whether bridge or regular). Perimeter + Bridge does not contain access levels or services: those are governed + entirely by the regular perimeter that resource is in. + + Perimeter Bridges are typically useful when building more complex + topologies with many independent perimeters that need to share some data + with a common perimeter, but should not be able to share data among + themselves. + values: + - :PERIMETER_TYPE_REGULAR + - :PERIMETER_TYPE_BRIDGE + default_value: :PERIMETER_TYPE_REGULAR + - !ruby/object:Api::Type::NestedObject + name: 'status' + description: | + ServicePerimeter configuration. Specifies sets of resources, + restricted services and access levels that determine + perimeter content and boundaries. + properties: + - !ruby/object:Api::Type::Array + name: 'resources' + description: | + A list of GCP resources that are inside of the service perimeter. + Currently only projects are allowed. + Format: projects/{project_number} + # TODO: (mbang) won't work for arrays yet, uncomment here once they are supported. + # (github.com/hashicorp/terraform-plugin-sdk/issues/470) + # at_least_one_of: + # - status.0.resources + # - status.0.access_levels + # - status.0.restricted_services + item_type: Api::Type::String + - !ruby/object:Api::Type::Array + name: 'accessLevels' + description: | + A list of AccessLevel resource names that allow resources within + the ServicePerimeter to be accessed from the internet. + AccessLevels listed must be in the same policy as this + ServicePerimeter. Referencing a nonexistent AccessLevel is a + syntax error. If no AccessLevel names are listed, resources within + the perimeter can only be accessed via GCP calls with request + origins within the perimeter. For Service Perimeter Bridge, must + be empty. + + Format: accessPolicies/{policy_id}/accessLevels/{access_level_name} + # TODO: (mbang) won't work for arrays yet, uncomment here once they are supported. + # (github.com/hashicorp/terraform-plugin-sdk/issues/470) + # at_least_one_of: + # - status.0.resources + # - status.0.access_levels + # - status.0.restricted_services + item_type: Api::Type::String + - !ruby/object:Api::Type::Array + name: 'restrictedServices' + description: | + GCP services that are subject to the Service Perimeter + restrictions. Must contain a list of services. For example, if + `storage.googleapis.com` is specified, access to the storage + buckets inside the perimeter must meet the perimeter's access + restrictions. + # TODO: (mbang) won't work for arrays yet, uncomment here once they are supported. + # (github.com/hashicorp/terraform-plugin-sdk/issues/470) + # at_least_one_of: + # - status.0.resources + # - status.0.access_levels + # - status.0.restricted_services + item_type: Api::Type::String + - !ruby/object:Api::Type::NestedObject + name: 'vpcAccessibleServices' + description: | + Specifies how APIs are allowed to communicate within the Service + Perimeter. + properties: + - !ruby/object:Api::Type::Boolean + name: 'enableRestriction' + description: | + Whether to restrict API calls within the Service Perimeter to the + list of APIs specified in 'allowedServices'. + - !ruby/object:Api::Type::Array + name: 'allowedServices' + description: | + The list of APIs usable within the Service Perimeter. + Must be empty unless `enableRestriction` is True. + item_type: Api::Type::String + - !ruby/object:Api::Type::NestedObject + name: 'spec' + description: | + Proposed (or dry run) ServicePerimeter configuration. + This configuration allows to specify and test ServicePerimeter configuration + without enforcing actual access restrictions. Only allowed to be set when + the `useExplicitDryRunSpec` flag is set. + properties: + - !ruby/object:Api::Type::Array + name: 'resources' + description: | + A list of GCP resources that are inside of the service perimeter. + Currently only projects are allowed. + Format: projects/{project_number} + # TODO: (mbang) won't work for arrays yet, uncomment here once they are supported. + # (github.com/hashicorp/terraform-plugin-sdk/issues/470) + # at_least_one_of: + # - status.0.resources + # - status.0.access_levels + # - status.0.restricted_services + item_type: Api::Type::String + - !ruby/object:Api::Type::Array + name: 'accessLevels' + description: | + A list of AccessLevel resource names that allow resources within + the ServicePerimeter to be accessed from the internet. + AccessLevels listed must be in the same policy as this + ServicePerimeter. Referencing a nonexistent AccessLevel is a + syntax error. If no AccessLevel names are listed, resources within + the perimeter can only be accessed via GCP calls with request + origins within the perimeter. For Service Perimeter Bridge, must + be empty. + + Format: accessPolicies/{policy_id}/accessLevels/{access_level_name} + # TODO: (mbang) won't work for arrays yet, uncomment here once they are supported. + # (github.com/hashicorp/terraform-plugin-sdk/issues/470) + # at_least_one_of: + # - status.0.resources + # - status.0.access_levels + # - status.0.restricted_services + item_type: Api::Type::String + - !ruby/object:Api::Type::Array + name: 'restrictedServices' + description: | + GCP services that are subject to the Service Perimeter + restrictions. Must contain a list of services. For example, if + `storage.googleapis.com` is specified, access to the storage + buckets inside the perimeter must meet the perimeter's access + restrictions. + # TODO: (mbang) won't work for arrays yet, uncomment here once they are supported. + # (github.com/hashicorp/terraform-plugin-sdk/issues/470) + # at_least_one_of: + # - status.0.resources + # - status.0.access_levels + # - status.0.restricted_services + item_type: Api::Type::String + - !ruby/object:Api::Type::NestedObject + name: 'vpcAccessibleServices' + description: | + Specifies how APIs are allowed to communicate within the Service + Perimeter. + properties: + - !ruby/object:Api::Type::Boolean + name: 'enableRestriction' + description: | + Whether to restrict API calls within the Service Perimeter to the + list of APIs specified in 'allowedServices'. + - !ruby/object:Api::Type::Array + name: 'allowedServices' + description: | + The list of APIs usable within the Service Perimeter. + Must be empty unless `enableRestriction` is True. + item_type: Api::Type::String + - !ruby/object:Api::Type::Boolean + name: 'useExplicitDryRunSpec' + description: | + Use explicit dry run spec flag. Ordinarily, a dry-run spec implicitly exists + for all Service Perimeters, and that spec is identical to the status for those + Service Perimeters. When this flag is set, it inhibits the generation of the + implicit spec, thereby allowing the user to explicitly provide a + configuration ("spec") to use in a dry-run version of the Service Perimeter. + This allows the user to test changes to the enforced config ("status") without + actually enforcing them. This testing is done through analyzing the differences + between currently enforced and suggested restrictions. useExplicitDryRunSpec must + bet set to True if any of the fields in the spec are set to non-default values. - !ruby/object:Api::Resource name: 'ServicePerimeterResource' create_url: "{{perimeter_name}}" diff --git a/products/accesscontextmanager/inspec.yaml b/products/accesscontextmanager/inspec.yaml index 1abaeac07a0a..c999744ed5bc 100644 --- a/products/accesscontextmanager/inspec.yaml +++ b/products/accesscontextmanager/inspec.yaml @@ -22,6 +22,8 @@ overrides: !ruby/object:Overrides::ResourceOverrides name_from_self_link: true AccessLevel: !ruby/object:Overrides::Inspec::ResourceOverride exclude: true + AccessLevels: !ruby/object:Overrides::Inspec::ResourceOverride + exclude: true ServicePerimeter: !ruby/object:Overrides::Inspec::ResourceOverride base_url: "accessPolicies/{{policy_name}}/servicePerimeters" self_link: "accessPolicies/{{policy_name}}/servicePerimeters/{{name}}" @@ -29,5 +31,7 @@ overrides: !ruby/object:Overrides::ResourceOverrides properties: name: !ruby/object:Overrides::Inspec::PropertyOverride name_from_self_link: true + ServicePerimeters: !ruby/object:Overrides::Inspec::ResourceOverride + exclude: true ServicePerimeterResource: !ruby/object:Overrides::Inspec::ResourceOverride exclude: true diff --git a/products/accesscontextmanager/terraform.yaml b/products/accesscontextmanager/terraform.yaml index 26a2283f9eb8..987516f77ce2 100644 --- a/products/accesscontextmanager/terraform.yaml +++ b/products/accesscontextmanager/terraform.yaml @@ -60,6 +60,34 @@ overrides: !ruby/object:Overrides::ResourceOverrides custom_code: !ruby/object:Provider::Terraform::CustomCode encoder: templates/terraform/encoders/access_level_never_send_parent.go.erb custom_import: templates/terraform/custom_import/set_access_policy_parent_from_self_link.go.erb + AccessLevels: !ruby/object:Overrides::Terraform::ResourceOverride + timeouts: !ruby/object:Api::Timeouts + insert_minutes: 6 + update_minutes: 6 + delete_minutes: 6 + autogen_async: true + # Skipping the sweeper due to the non-standard base_url + skip_sweeper: true + id_format: "{{parent}}/accessLevels" + import_format: ["{{parent}}/accessLevels"] + examples: + - !ruby/object:Provider::Terraform::Examples + name: "access_context_manager_access_levels_basic" + skip_test: true + primary_resource_id: "access-levels" + vars: + access_level_name1: "chromeos_no_lock" + access_level_name2: "mac_no_lock" + properties: + parent: !ruby/object:Overrides::Terraform::PropertyOverride + ignore_read: true + accessLevels: !ruby/object:Overrides::Terraform::PropertyOverride + is_set: true + accessLevels.basic.combiningFunction: !ruby/object:Overrides::Terraform::PropertyOverride + custom_flatten: templates/terraform/custom_flatten/default_if_empty.erb + custom_code: !ruby/object:Provider::Terraform::CustomCode + custom_import: templates/terraform/custom_import/set_access_policy_parent_from_access_policy.go.erb + custom_delete: templates/terraform/custom_delete/replace_all_access_levels_empty_list.go.erb ServicePerimeter: !ruby/object:Overrides::Terraform::ResourceOverride timeouts: !ruby/object:Api::Timeouts insert_minutes: 6 @@ -98,6 +126,39 @@ overrides: !ruby/object:Overrides::ResourceOverrides custom_code: !ruby/object:Provider::Terraform::CustomCode encoder: templates/terraform/encoders/access_level_never_send_parent.go.erb custom_import: templates/terraform/custom_import/set_access_policy_parent_from_self_link.go.erb + ServicePerimeters: !ruby/object:Overrides::Terraform::ResourceOverride + timeouts: !ruby/object:Api::Timeouts + insert_minutes: 6 + update_minutes: 6 + delete_minutes: 6 + autogen_async: true + # Skipping the sweeper due to the non-standard base_url + skip_sweeper: true + id_format: "{{parent}}/servicePerimeters" + import_format: ["{{parent}}/servicePerimeters"] + examples: + - !ruby/object:Provider::Terraform::Examples + name: "access_context_manager_service_perimeters_basic" + skip_test: true + primary_resource_id: "service-perimeter" + vars: + access_level_name: "chromeos_no_lock" + service_perimeter_name: "restrict_storage" + properties: + parent: !ruby/object:Overrides::Terraform::PropertyOverride + ignore_read: true + servicePerimeters: !ruby/object:Overrides::Terraform::PropertyOverride + is_set: true + servicePerimeters.perimeterType: !ruby/object:Overrides::Terraform::PropertyOverride + custom_flatten: templates/terraform/custom_flatten/default_if_empty.erb + input: true + servicePerimeters.status.restrictedServices: !ruby/object:Overrides::Terraform::PropertyOverride + is_set: true + servicePerimeters.status.vpcAccessibleServices.allowedServices: !ruby/object:Overrides::Terraform::PropertyOverride + is_set: true + custom_code: !ruby/object:Provider::Terraform::CustomCode + custom_import: templates/terraform/custom_import/set_access_policy_parent_from_access_policy.go.erb + custom_delete: templates/terraform/custom_delete/replace_all_service_perimeters_empty_list.go.erb ServicePerimeterResource: !ruby/object:Overrides::Terraform::ResourceOverride autogen_async: true exclude_validator: true diff --git a/templates/terraform/custom_delete/replace_all_access_levels_empty_list.go.erb b/templates/terraform/custom_delete/replace_all_access_levels_empty_list.go.erb new file mode 100644 index 000000000000..9006bd1e7408 --- /dev/null +++ b/templates/terraform/custom_delete/replace_all_access_levels_empty_list.go.erb @@ -0,0 +1,28 @@ +config := meta.(*Config) + +obj := make(map[string]interface{}) +obj["accessLevels"] = []string{} + +url, err := replaceVars(d, config, "{{AccessContextManagerBasePath}}{{parent}}/accessLevels:replaceAll") +if err != nil { + return err +} + +log.Printf("[DEBUG] Deleting AccessLevels %q: %#v", d.Id(), obj) +res, err := sendRequestWithTimeout(config, "POST", "", url, obj, d.Timeout(schema.TimeoutUpdate)) + +if err != nil { + return fmt.Errorf("Error deleting AccessLevels %q: %s", d.Id(), err) +} else { + log.Printf("[DEBUG] Finished deleting AccessLevels %q: %#v", d.Id(), res) +} + +err = accessContextManagerOperationWaitTime( + config, res, "Updating AccessLevels", + d.Timeout(schema.TimeoutUpdate)) + +if err != nil { + return err +} + +return nil \ No newline at end of file diff --git a/templates/terraform/custom_delete/replace_all_service_perimeters_empty_list.go.erb b/templates/terraform/custom_delete/replace_all_service_perimeters_empty_list.go.erb new file mode 100644 index 000000000000..69b144fd257d --- /dev/null +++ b/templates/terraform/custom_delete/replace_all_service_perimeters_empty_list.go.erb @@ -0,0 +1,28 @@ +config := meta.(*Config) + +obj := make(map[string]interface{}) +obj["servicePerimeters"] = []string{} + +url, err := replaceVars(d, config, "{{AccessContextManagerBasePath}}{{parent}}/servicePerimeters:replaceAll") +if err != nil { + return err +} + +log.Printf("[DEBUG] Deleting servicePerimeters %q: %#v", d.Id(), obj) +res, err := sendRequestWithTimeout(config, "POST", "", url, obj, d.Timeout(schema.TimeoutUpdate)) + +if err != nil { + return fmt.Errorf("Error deleting ServicePerimeters %q: %s", d.Id(), err) +} else { + log.Printf("[DEBUG] Finished deleting ServicePerimeters %q: %#v", d.Id(), res) +} + +err = accessContextManagerOperationWaitTime( + config, res, "Updating ServicePerimeters", + d.Timeout(schema.TimeoutUpdate)) + +if err != nil { + return err +} + +return nil \ No newline at end of file diff --git a/templates/terraform/custom_import/set_access_policy_parent_from_access_policy.go.erb b/templates/terraform/custom_import/set_access_policy_parent_from_access_policy.go.erb new file mode 100644 index 000000000000..012d692d62da --- /dev/null +++ b/templates/terraform/custom_import/set_access_policy_parent_from_access_policy.go.erb @@ -0,0 +1,24 @@ +<%# The license inside this block applies to this file. + # Copyright 2020 Google Inc. + # Licensed under the Apache License, Version 2.0 (the "License"); + # you may not use this file except in compliance with the License. + # You may obtain a copy of the License at + # + # http://www.apache.org/licenses/LICENSE-2.0 + # + # Unless required by applicable law or agreed to in writing, software + # distributed under the License is distributed on an "AS IS" BASIS, + # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + # See the License for the specific language governing permissions and + # limitations under the License. +-%> + config := meta.(*Config) + + // current import_formats can't import fields with forward slashes in their value + parts, err := getImportIdQualifiers([]string{"accessPolicies/(?P[^/]+)/(.+)"}, d, config, d.Id()) + if err != nil { + return nil, err + } + + d.Set("parent", fmt.Sprintf("accessPolicies/%s", parts["accessPolicy"])) + return []*schema.ResourceData{d}, nil diff --git a/templates/terraform/examples/access_context_manager_access_levels_basic.tf.erb b/templates/terraform/examples/access_context_manager_access_levels_basic.tf.erb new file mode 100644 index 000000000000..b1ec2715cf22 --- /dev/null +++ b/templates/terraform/examples/access_context_manager_access_levels_basic.tf.erb @@ -0,0 +1,47 @@ +resource "google_access_context_manager_access_levels" "<%= ctx[:primary_resource_id] %>" { + parent = "accessPolicies/${google_access_context_manager_access_policy.access-policy.name}" + access_levels { + name = "accessPolicies/${google_access_context_manager_access_policy.access-policy.name}/accessLevels/<%= ctx[:vars]['access_level_name1'] %>" + title = "<%= ctx[:vars]['access_level_name1'] %>" + basic { + conditions { + device_policy { + require_screen_lock = true + os_constraints { + os_type = "DESKTOP_CHROME_OS" + } + } + regions = [ + "CH", + "IT", + "US", + ] + } + } + } + + access_levels { + name = "accessPolicies/${google_access_context_manager_access_policy.access-policy.name}/accessLevels/<%= ctx[:vars]['access_level_name2'] %>" + title = "<%= ctx[:vars]['access_level_name2'] %>" + basic { + conditions { + device_policy { + require_screen_lock = true + os_constraints { + os_type = "DESKTOP_MAC" + } + } + regions = [ + "CH", + "IT", + "US", + ] + } + } + } +} + +resource "google_access_context_manager_access_policy" "access-policy" { + parent = "organizations/123456789" + title = "my policy" +} diff --git a/templates/terraform/examples/access_context_manager_service_perimeters_basic.tf.erb b/templates/terraform/examples/access_context_manager_service_perimeters_basic.tf.erb new file mode 100644 index 000000000000..7fc86677dc07 --- /dev/null +++ b/templates/terraform/examples/access_context_manager_service_perimeters_basic.tf.erb @@ -0,0 +1,45 @@ +resource "google_access_context_manager_service_perimeters" "<%= ctx[:primary_resource_id] %>" { + parent = "accessPolicies/${google_access_context_manager_access_policy.access-policy.name}" + + service_perimeters { + name = "accessPolicies/${google_access_context_manager_access_policy.access-policy.name}/servicePerimeters/<%= ctx[:vars]['service_perimeter_name1'] %>" + title = "<%= ctx[:vars]['service_perimeter_name1'] %>" + status { + restricted_services = ["storage.googleapis.com"] + } + } + + service_perimeters { + name = "accessPolicies/${google_access_context_manager_access_policy.access-policy.name}/servicePerimeters/<%= ctx[:vars]['service_perimeter_name2'] %>" + title = "<%= ctx[:vars]['service_perimeter_name2'] %>" + status { + restricted_services = ["bigtable.googleapis.com"] + } + } +} + +resource "google_access_context_manager_access_level" "access-level" { + parent = "accessPolicies/${google_access_context_manager_access_policy.access-policy.name}" + name = "accessPolicies/${google_access_context_manager_access_policy.access-policy.name}/accessLevels/<%= ctx[:vars]['access_level_name'] %>" + title = "<%= ctx[:vars]['access_level_name'] %>" + basic { + conditions { + device_policy { + require_screen_lock = false + os_constraints { + os_type = "DESKTOP_CHROME_OS" + } + } + regions = [ + "CH", + "IT", + "US", + ] + } + } +} + +resource "google_access_context_manager_access_policy" "access-policy" { + parent = "organizations/123456789" + title = "my policy" +} diff --git a/third_party/terraform/tests/resource_access_context_manager_access_levels_test.go b/third_party/terraform/tests/resource_access_context_manager_access_levels_test.go new file mode 100644 index 000000000000..52208fa1686d --- /dev/null +++ b/third_party/terraform/tests/resource_access_context_manager_access_levels_test.go @@ -0,0 +1,157 @@ +package google + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/terraform" +) + +// Since each test here is acting on the same organization and only one AccessPolicy +// can exist, they need to be ran serially. See AccessPolicy for the test runner. + +func testAccAccessContextManagerAccessLevels_basicTest(t *testing.T) { + org := getTestOrgFromEnv(t) + + vcrTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAccessContextManagerAccessLevelsDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccAccessContextManagerAccessLevels_basic(org, "my policy", "corpnet_access", "prodnet_access"), + }, + { + ResourceName: "google_access_context_manager_access_levels.test-access", + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccAccessContextManagerAccessLevels_basicUpdated(org, "my new policy", "corpnet_access", "prodnet_access"), + }, + { + ResourceName: "google_access_context_manager_access_levels.test-access", + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccAccessContextManagerAccessLevel_empty(org, "my new policy"), + }, + { + ResourceName: "google_access_context_manager_access_levels.test-access", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func testAccCheckAccessContextManagerAccessLevelsDestroyProducer(t *testing.T) func(s *terraform.State) error { + return func(s *terraform.State) error { + for _, rs := range s.RootModule().Resources { + if rs.Type != "google_access_context_manager_access_levels" { + continue + } + + config := googleProviderConfig(t) + + url, err := replaceVarsForTest(config, rs, "{{AccessContextManagerBasePath}}{{parent}}/accessLevels") + if err != nil { + return err + } + + _, err = sendRequest(config, "GET", "", url, nil) + if err == nil { + return fmt.Errorf("AccessLevels still exists at %s", url) + } + } + + return nil + } +} + +func testAccAccessContextManagerAccessLevels_basic(org, policyTitle, levelTitleName1, levelTitleName2 string) string { + return fmt.Sprintf(` +resource "google_access_context_manager_access_policy" "test-access" { + parent = "organizations/%s" + title = "%s" +} + +resource "google_access_context_manager_access_levels" "test-access" { + parent = "accessPolicies/${google_access_context_manager_access_policy.test-access.name}" + + access_levels { + name = "accessPolicies/${google_access_context_manager_access_policy.test-access.name}/accessLevels/%s" + title = "%s" + description = "hello" + basic { + combining_function = "AND" + conditions { + ip_subnetworks = ["192.0.4.0/24"] + } + } + } + + access_levels { + name = "accessPolicies/${google_access_context_manager_access_policy.test-access.name}/accessLevels/%s" + title = "%s" + description = "hello again" + basic { + conditions { + ip_subnetworks = ["176.0.2.0/24"] + } + } + } +} +`, org, policyTitle, levelTitleName1, levelTitleName1, levelTitleName2, levelTitleName2) +} + +func testAccAccessContextManagerAccessLevels_basicUpdated(org, policyTitle, levelTitleName1, levelTitleName2 string) string { + return fmt.Sprintf(` +resource "google_access_context_manager_access_policy" "test-access" { + parent = "organizations/%s" + title = "%s" +} + +resource "google_access_context_manager_access_levels" "test-access" { + parent = "accessPolicies/${google_access_context_manager_access_policy.test-access.name}" + + access_levels { + name = "accessPolicies/${google_access_context_manager_access_policy.test-access.name}/accessLevels/%s" + title = "%s" + description = "hello" + basic { + combining_function = "AND" + conditions { + ip_subnetworks = ["192.0.2.0/24"] + } + } + } + + access_levels { + name = "accessPolicies/${google_access_context_manager_access_policy.test-access.name}/accessLevels/%s" + title = "%s" + description = "hello again" + basic { + conditions { + ip_subnetworks = ["176.0.4.0/24"] + } + } + } +} +`, org, policyTitle, levelTitleName1, levelTitleName1, levelTitleName2, levelTitleName2) +} + +func testAccAccessContextManagerAccessLevel_empty(org, policyTitle string) string { + return fmt.Sprintf(` +resource "google_access_context_manager_access_policy" "test-access" { + parent = "organizations/%s" + title = "%s" +} + +resource "google_access_context_manager_access_levels" "test-access" { + parent = "accessPolicies/${google_access_context_manager_access_policy.test-access.name}" +} +`, org, policyTitle) +} diff --git a/third_party/terraform/tests/resource_access_context_manager_access_policy_test.go.erb b/third_party/terraform/tests/resource_access_context_manager_access_policy_test.go.erb index 8c30b957ea69..8a6cc6ef07b9 100644 --- a/third_party/terraform/tests/resource_access_context_manager_access_policy_test.go.erb +++ b/third_party/terraform/tests/resource_access_context_manager_access_policy_test.go.erb @@ -85,6 +85,8 @@ func TestAccAccessContextManager(t *testing.T) { "access_level": testAccAccessContextManagerAccessLevel_basicTest, "access_level_full": testAccAccessContextManagerAccessLevel_fullTest, "access_level_custom": testAccAccessContextManagerAccessLevel_customTest, + "access_levels": testAccAccessContextManagerAccessLevels_basicTest, + "service_perimeters": testAccAccessContextManagerServicePerimeters_basicTest, } for name, tc := range testCases { diff --git a/third_party/terraform/tests/resource_access_context_manager_services_perimeters_test.go b/third_party/terraform/tests/resource_access_context_manager_services_perimeters_test.go new file mode 100644 index 000000000000..1406cc64b8fc --- /dev/null +++ b/third_party/terraform/tests/resource_access_context_manager_services_perimeters_test.go @@ -0,0 +1,196 @@ +package google + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/terraform" +) + +// Since each test here is acting on the same organization and only one AccessPolicy +// can exist, they need to be ran serially. See AccessPolicy for the test runner. +func testAccAccessContextManagerServicePerimeters_basicTest(t *testing.T) { + org := getTestOrgFromEnv(t) + + vcrTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAccessContextManagerServicePerimetersDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccAccessContextManagerServicePerimeters_basic(org, "my policy", "level", "storage_perimeter", "bigtable_perimeter"), + }, + { + ResourceName: "google_access_context_manager_service_perimeters.test-access", + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccAccessContextManagerServicePerimeters_update(org, "my policy", "level", "storage_perimeter", "bigquery_perimeter", "bigtable_perimeter"), + }, + { + ResourceName: "google_access_context_manager_service_perimeters.test-access", + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccAccessContextManagerServicePerimeters_empty(org, "my policy", "level"), + }, + { + ResourceName: "google_access_context_manager_service_perimeters.test-access", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func testAccCheckAccessContextManagerServicePerimetersDestroyProducer(t *testing.T) func(s *terraform.State) error { + return func(s *terraform.State) error { + for _, rs := range s.RootModule().Resources { + if rs.Type != "google_access_context_manager_service_perimeters" { + continue + } + + config := googleProviderConfig(t) + + url, err := replaceVarsForTest(config, rs, "{{AccessContextManagerBasePath}}{{parent}}/servicePerimeters") + if err != nil { + return err + } + + _, err = sendRequest(config, "GET", "", url, nil) + if err == nil { + return fmt.Errorf("ServicePerimeters still exists at %s", url) + } + } + + return nil + } +} + +func testAccAccessContextManagerServicePerimeters_basic(org, policyTitle, levelTitleName, perimeterTitleName1, perimeterTitleName2 string) string { + return fmt.Sprintf(` +resource "google_access_context_manager_access_policy" "test-access" { + parent = "organizations/%s" + title = "%s" +} + +resource "google_access_context_manager_access_level" "test-access" { + parent = "accessPolicies/${google_access_context_manager_access_policy.test-access.name}" + name = "accessPolicies/${google_access_context_manager_access_policy.test-access.name}/accessLevels/%s" + title = "%s" + description = "hello" + basic { + combining_function = "AND" + conditions { + ip_subnetworks = ["192.0.4.0/24"] + } + } +} + +resource "google_access_context_manager_service_perimeters" "test-access" { + parent = "accessPolicies/${google_access_context_manager_access_policy.test-access.name}" + + service_perimeters { + name = "accessPolicies/${google_access_context_manager_access_policy.test-access.name}/servicePerimeters/%s" + title = "%s" + perimeter_type = "PERIMETER_TYPE_REGULAR" + status { + restricted_services = ["storage.googleapis.com"] + } + } + + service_perimeters { + name = "accessPolicies/${google_access_context_manager_access_policy.test-access.name}/servicePerimeters/%s" + title = "%s" + perimeter_type = "PERIMETER_TYPE_REGULAR" + status { + restricted_services = ["bigtable.googleapis.com"] + } + } +} +`, org, policyTitle, levelTitleName, levelTitleName, perimeterTitleName1, perimeterTitleName1, perimeterTitleName2, perimeterTitleName2) +} + +func testAccAccessContextManagerServicePerimeters_update(org, policyTitle, levelTitleName, perimeterTitleName1, perimeterTitleName2, perimeterTitleName3 string) string { + return fmt.Sprintf(` +resource "google_access_context_manager_access_policy" "test-access" { + parent = "organizations/%s" + title = "%s" +} + +resource "google_access_context_manager_access_level" "test-access" { + parent = "accessPolicies/${google_access_context_manager_access_policy.test-access.name}" + name = "accessPolicies/${google_access_context_manager_access_policy.test-access.name}/accessLevels/%s" + title = "%s" + description = "hello" + basic { + combining_function = "AND" + conditions { + ip_subnetworks = ["192.0.4.0/24"] + } + } +} + +resource "google_access_context_manager_service_perimeters" "test-access" { + parent = "accessPolicies/${google_access_context_manager_access_policy.test-access.name}" + + service_perimeters { + name = "accessPolicies/${google_access_context_manager_access_policy.test-access.name}/servicePerimeters/%s" + title = "%s" + perimeter_type = "PERIMETER_TYPE_REGULAR" + status { + restricted_services = ["storage.googleapis.com"] + access_levels = [google_access_context_manager_access_level.test-access.name] + } + } + + service_perimeters { + name = "accessPolicies/${google_access_context_manager_access_policy.test-access.name}/servicePerimeters/%s" + title = "%s" + perimeter_type = "PERIMETER_TYPE_REGULAR" + status { + restricted_services = ["bigquery.googleapis.com"] + access_levels = [google_access_context_manager_access_level.test-access.name] + } + } + + service_perimeters { + name = "accessPolicies/${google_access_context_manager_access_policy.test-access.name}/servicePerimeters/%s" + title = "%s" + perimeter_type = "PERIMETER_TYPE_REGULAR" + status { + restricted_services = ["bigtable.googleapis.com"] + } + } +} +`, org, policyTitle, levelTitleName, levelTitleName, perimeterTitleName1, perimeterTitleName1, perimeterTitleName2, perimeterTitleName2, perimeterTitleName3, perimeterTitleName3) +} + +func testAccAccessContextManagerServicePerimeters_empty(org, policyTitle, levelTitleName string) string { + return fmt.Sprintf(` +resource "google_access_context_manager_access_policy" "test-access" { + parent = "organizations/%s" + title = "%s" +} + +resource "google_access_context_manager_access_level" "test-access" { + parent = "accessPolicies/${google_access_context_manager_access_policy.test-access.name}" + name = "accessPolicies/${google_access_context_manager_access_policy.test-access.name}/accessLevels/%s" + title = "%s" + description = "hello" + basic { + combining_function = "AND" + conditions { + ip_subnetworks = ["192.0.4.0/24"] + } + } +} + +resource "google_access_context_manager_service_perimeters" "test-access" { + parent = "accessPolicies/${google_access_context_manager_access_policy.test-access.name}" +} +`, org, policyTitle, levelTitleName, levelTitleName) +}