Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added code for Apigee Environments #4553

Merged
merged 4 commits into from
Mar 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 57 additions & 1 deletion mmv1/products/apigee/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,60 @@ objects:
guides:
'Creating a runtime instance':
'https://cloud.google.com/apigee/docs/api-platform/get-started/create-instance'
api: 'https://cloud.google.com/apigee/docs/reference/apis/apigee/rest/v1/organizations.instances/create'
api: 'https://cloud.google.com/apigee/docs/reference/apis/apigee/rest/v1/organizations.instances/create'
- !ruby/object:Api::Resource
name: 'Environment'
base_url: 'environments'
create_url: '{{org_id}}/environments'
self_link: '{{org_id}}/environments/{{name}}'
async: !ruby/object:Api::OpAsync
operation: !ruby/object:Api::OpAsync::Operation
path: 'name'
base_url: '{{op_id}}'
wait_ms: 1000
result: !ruby/object:Api::OpAsync::Result
path: 'response'
resource_inside_response: true
status: !ruby/object:Api::OpAsync::Status
path: 'done'
complete: True
allowed:
- True
- False
error: !ruby/object:Api::OpAsync::Error
path: 'error'
message: 'message'
description: |
An `Environment` in Apigee.
parameters:
- !ruby/object:Api::Type::String
name: 'orgId'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the API docs specify this as parent - why does orgId work? https://cloud.google.com/apigee/docs/reference/apis/apigee/rest/v1/organizations.environments/create

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parent is just a URL parameter, so we can call it however we want. We actually have the two models in other resources. For example, the older google_access_context_manager_access_level used parent as is, while the newer google_kms_crypto_key uses the parent as key_ring. The second (with an explicit field name that matches what is expected) seems more readable.

description: |
The Apigee Organization associated with the Apigee environment,
in the format `organizations/{{org_name}}`.
required: true
input: true
url_param_only: true
properties:
- !ruby/object:Api::Type::String
name: 'name'
description: |
The resource ID of the environment.
required: true
- !ruby/object:Api::Type::String
name: 'displayName'
description: |
Display name of the environment.
required: false
input: true
- !ruby/object:Api::Type::String
name: 'description'
description: |
Description of the environment.
required: false
input: true
references: !ruby/object:Api::Resource::ReferenceLinks
guides:
'Creating an environment':
'https://cloud.google.com/apigee/docs/api-platform/get-started/create-environment'
api: 'https://cloud.google.com/apigee/docs/reference/apis/apigee/rest/v1/organizations.environments/create'
23 changes: 23 additions & 0 deletions mmv1/products/apigee/terraform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,29 @@ overrides: !ruby/object:Overrides::ResourceOverrides
delete_minutes: 60
custom_code: !ruby/object:Provider::Terraform::CustomCode
custom_import: templates/terraform/custom_import/apigee_instance.go.erb
Environment: !ruby/object:Overrides::Terraform::ResourceOverride
autogen_async: true
import_format: ["{{org_id}}/environments/{{name}}", "{{org_id}}/{{name}}"]
examples:
- !ruby/object:Provider::Terraform::Examples
name: "apigee_environment_basic"
skip_test: true
- !ruby/object:Provider::Terraform::Examples
# This is a more verbose version of the above that creates all
# the resources needed for the acceptance test.
name: "apigee_environment_basic_test"
primary_resource_id: "apigee_environment"
test_env_vars:
org_id: :ORG_ID
billing_account: :BILLING_ACCT
skip_docs: true
# Resource creation race
skip_vcr: true
timeouts: !ruby/object:Api::Timeouts
insert_minutes: 30
delete_minutes: 30
custom_code: !ruby/object:Provider::Terraform::CustomCode
custom_import: templates/terraform/custom_import/apigee_environment.go.erb
files: !ruby/object:Provider::Config::Files
# These files have templating (ERB) code that will be run.
# This is usually to add licensing info, autogeneration notices, etc.
Expand Down
42 changes: 42 additions & 0 deletions mmv1/templates/terraform/custom_import/apigee_environment.go.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
config := meta.(*Config)

// current import_formats cannot import fields with forward slashes in their value
if err := parseImportId([]string{"(?P<name>.+)"}, d, config); err != nil {
return nil, err
}

nameParts := strings.Split(d.Get("name").(string), "/")
if len(nameParts) == 4 {
// `organizations/{{org_name}}/environments/{{name}}`
orgId := fmt.Sprintf("organizations/%s", nameParts[1])
if err := d.Set("org_id", orgId); err != nil {
return nil, fmt.Errorf("Error setting org_id: %s", err)
}
if err := d.Set("name", nameParts[3]); err != nil {
return nil, fmt.Errorf("Error setting name: %s", err)
}
} else if len(nameParts) == 3 {
// `organizations/{{org_name}}/{{name}}`
orgId := fmt.Sprintf("organizations/%s", nameParts[1])
if err := d.Set("org_id", orgId); err != nil {
return nil, fmt.Errorf("Error setting org_id: %s", err)
}
if err := d.Set("name", nameParts[2]); err != nil {
return nil, fmt.Errorf("Error setting name: %s", err)
}
} else {
return nil, fmt.Errorf(
"Saw %s when the name is expected to have shape %s or %s",
d.Get("name"),
"organizations/{{org_name}}/environments/{{name}}",
"organizations/{{org_name}}/{{name}}")
}

// Replace import id for the resource id
id, err := replaceVars(d, config, "{{org_id}}/environments/{{name}}")
if err != nil {
return nil, fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)

return []*schema.ResourceData{d}, nil
34 changes: 34 additions & 0 deletions mmv1/templates/terraform/examples/apigee_environment_basic.tf.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
data "google_client_config" "current" {}

resource "google_compute_network" "apigee_network" {
name = "apigee-network"
}

resource "google_compute_global_address" "apigee_range" {
name = "apigee-range"
purpose = "VPC_PEERING"
address_type = "INTERNAL"
prefix_length = 16
network = google_compute_network.apigee_network.id
}

resource "google_service_networking_connection" "apigee_vpc_connection" {
network = google_compute_network.apigee_network.id
service = "servicenetworking.googleapis.com"
reserved_peering_ranges = [google_compute_global_address.apigee_range.name]
}

resource "google_apigee_organization" "apigee_org" {
analytics_region = "us-central1"
project_id = data.google_client_config.current.project
authorized_network = google_compute_network.apigee_network.id
depends_on = [google_service_networking_connection.apigee_vpc_connection]
}

resource "google_apigee_environment" "env" {
name = "tf-test%{random_suffix}"
description = "Apigee Environment"
displayName = "environment-1"
org_id = google_apigee_organization.apigee_org.id
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
resource "google_project" "project" {
project_id = "tf-test%{random_suffix}"
name = "tf-test%{random_suffix}"
org_id = "<%= ctx[:test_env_vars]['org_id'] %>"
billing_account = "<%= ctx[:test_env_vars]['billing_account'] %>"
}

resource "google_project_service" "apigee" {
project = google_project.project.project_id
service = "apigee.googleapis.com"
}

resource "google_project_service" "compute" {
project = google_project.project.project_id
service = "compute.googleapis.com"
}

resource "google_project_service" "servicenetworking" {
project = google_project.project.project_id
service = "servicenetworking.googleapis.com"
}

resource "google_compute_network" "apigee_network" {
name = "apigee-network"
project = google_project.project.project_id
depends_on = [google_project_service.compute]
}

resource "google_compute_global_address" "apigee_range" {
name = "apigee-range"
purpose = "VPC_PEERING"
address_type = "INTERNAL"
prefix_length = 16
network = google_compute_network.apigee_network.id
project = google_project.project.project_id
}

resource "google_service_networking_connection" "apigee_vpc_connection" {
network = google_compute_network.apigee_network.id
service = "servicenetworking.googleapis.com"
reserved_peering_ranges = [google_compute_global_address.apigee_range.name]
depends_on = [google_project_service.servicenetworking]
}

resource "google_apigee_organization" "apigee_org" {
analytics_region = "us-central1"
project_id = google_project.project.project_id
authorized_network = google_compute_network.apigee_network.id
depends_on = [
google_service_networking_connection.apigee_vpc_connection,
google_project_service.apigee,
]
}

resource "google_apigee_environment" "<%= ctx[:primary_resource_id] %>" {
org_id = google_apigee_organization.apigee_org.id
name = "tf-test%{random_suffix}"
description = "Apigee Environment"
display_name = "environment-1"
}