Skip to content

Commit e768704

Browse files
Add properties field to apigee environment resource (#12752)
Signed-off-by: Lagu22 <[email protected]> [upstream:ce015c1ec274a48425efd42b953dab3e4044ecaa] Signed-off-by: Modular Magician <[email protected]>
1 parent e5cdff9 commit e768704

5 files changed

+287
-0
lines changed

.changelog/12752.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
apigee: added `properties` field to `google_apigee_environment` resource
3+
```

google/services/apigee/resource_apigee_environment.go

+144
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,35 @@ all instances.`,
132132
},
133133
},
134134
},
135+
"properties": {
136+
Type: schema.TypeList,
137+
Optional: true,
138+
Description: `Key-value pairs that may be used for customizing the environment.`,
139+
MaxItems: 1,
140+
Elem: &schema.Resource{
141+
Schema: map[string]*schema.Schema{
142+
"property": {
143+
Type: schema.TypeList,
144+
Optional: true,
145+
Description: `List of all properties in the object.`,
146+
Elem: &schema.Resource{
147+
Schema: map[string]*schema.Schema{
148+
"name": {
149+
Type: schema.TypeString,
150+
Optional: true,
151+
Description: `The property key.`,
152+
},
153+
"value": {
154+
Type: schema.TypeString,
155+
Optional: true,
156+
Description: `The property value.`,
157+
},
158+
},
159+
},
160+
},
161+
},
162+
},
163+
},
135164
"type": {
136165
Type: schema.TypeString,
137166
Computed: true,
@@ -203,6 +232,12 @@ func resourceApigeeEnvironmentCreate(d *schema.ResourceData, meta interface{}) e
203232
} else if v, ok := d.GetOkExists("forward_proxy_uri"); !tpgresource.IsEmptyValue(reflect.ValueOf(forwardProxyUriProp)) && (ok || !reflect.DeepEqual(v, forwardProxyUriProp)) {
204233
obj["forwardProxyUri"] = forwardProxyUriProp
205234
}
235+
propertiesProp, err := expandApigeeEnvironmentProperties(d.Get("properties"), d, config)
236+
if err != nil {
237+
return err
238+
} else if v, ok := d.GetOkExists("properties"); !tpgresource.IsEmptyValue(reflect.ValueOf(propertiesProp)) && (ok || !reflect.DeepEqual(v, propertiesProp)) {
239+
obj["properties"] = propertiesProp
240+
}
206241

207242
url, err := tpgresource.ReplaceVars(d, config, "{{ApigeeBasePath}}{{org_id}}/environments")
208243
if err != nil {
@@ -324,6 +359,9 @@ func resourceApigeeEnvironmentRead(d *schema.ResourceData, meta interface{}) err
324359
if err := d.Set("forward_proxy_uri", flattenApigeeEnvironmentForwardProxyUri(res["forwardProxyUri"], d, config)); err != nil {
325360
return fmt.Errorf("Error reading Environment: %s", err)
326361
}
362+
if err := d.Set("properties", flattenApigeeEnvironmentProperties(res["properties"], d, config)); err != nil {
363+
return fmt.Errorf("Error reading Environment: %s", err)
364+
}
327365

328366
return nil
329367
}
@@ -368,6 +406,12 @@ func resourceApigeeEnvironmentUpdate(d *schema.ResourceData, meta interface{}) e
368406
} else if v, ok := d.GetOkExists("forward_proxy_uri"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, forwardProxyUriProp)) {
369407
obj["forwardProxyUri"] = forwardProxyUriProp
370408
}
409+
propertiesProp, err := expandApigeeEnvironmentProperties(d.Get("properties"), d, config)
410+
if err != nil {
411+
return err
412+
} else if v, ok := d.GetOkExists("properties"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, propertiesProp)) {
413+
obj["properties"] = propertiesProp
414+
}
371415

372416
url, err := tpgresource.ReplaceVars(d, config, "{{ApigeeBasePath}}{{org_id}}/environments/{{name}}")
373417
if err != nil {
@@ -397,6 +441,10 @@ func resourceApigeeEnvironmentUpdate(d *schema.ResourceData, meta interface{}) e
397441
if d.HasChange("forward_proxy_uri") {
398442
updateMask = append(updateMask, "forwardProxyUri")
399443
}
444+
445+
if d.HasChange("properties") {
446+
updateMask = append(updateMask, "properties")
447+
}
400448
// updateMask is a URL parameter but not present in the schema, so ReplaceVars
401449
// won't set it
402450
url, err = transport_tpg.AddQueryParams(url, map[string]string{"updateMask": strings.Join(updateMask, ",")})
@@ -585,6 +633,46 @@ func flattenApigeeEnvironmentForwardProxyUri(v interface{}, d *schema.ResourceDa
585633
return v
586634
}
587635

636+
func flattenApigeeEnvironmentProperties(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
637+
if v == nil {
638+
return nil
639+
}
640+
original := v.(map[string]interface{})
641+
if len(original) == 0 {
642+
return nil
643+
}
644+
transformed := make(map[string]interface{})
645+
transformed["property"] =
646+
flattenApigeeEnvironmentPropertiesProperty(original["property"], d, config)
647+
return []interface{}{transformed}
648+
}
649+
func flattenApigeeEnvironmentPropertiesProperty(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
650+
if v == nil {
651+
return v
652+
}
653+
l := v.([]interface{})
654+
transformed := make([]interface{}, 0, len(l))
655+
for _, raw := range l {
656+
original := raw.(map[string]interface{})
657+
if len(original) < 1 {
658+
// Do not include empty json objects coming back from the api
659+
continue
660+
}
661+
transformed = append(transformed, map[string]interface{}{
662+
"name": flattenApigeeEnvironmentPropertiesPropertyName(original["name"], d, config),
663+
"value": flattenApigeeEnvironmentPropertiesPropertyValue(original["value"], d, config),
664+
})
665+
}
666+
return transformed
667+
}
668+
func flattenApigeeEnvironmentPropertiesPropertyName(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
669+
return v
670+
}
671+
672+
func flattenApigeeEnvironmentPropertiesPropertyValue(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
673+
return v
674+
}
675+
588676
func expandApigeeEnvironmentName(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
589677
return v, nil
590678
}
@@ -657,3 +745,59 @@ func expandApigeeEnvironmentType(v interface{}, d tpgresource.TerraformResourceD
657745
func expandApigeeEnvironmentForwardProxyUri(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
658746
return v, nil
659747
}
748+
749+
func expandApigeeEnvironmentProperties(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
750+
l := v.([]interface{})
751+
if len(l) == 0 || l[0] == nil {
752+
return nil, nil
753+
}
754+
raw := l[0]
755+
original := raw.(map[string]interface{})
756+
transformed := make(map[string]interface{})
757+
758+
transformedProperty, err := expandApigeeEnvironmentPropertiesProperty(original["property"], d, config)
759+
if err != nil {
760+
return nil, err
761+
} else if val := reflect.ValueOf(transformedProperty); val.IsValid() && !tpgresource.IsEmptyValue(val) {
762+
transformed["property"] = transformedProperty
763+
}
764+
765+
return transformed, nil
766+
}
767+
768+
func expandApigeeEnvironmentPropertiesProperty(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
769+
l := v.([]interface{})
770+
req := make([]interface{}, 0, len(l))
771+
for _, raw := range l {
772+
if raw == nil {
773+
continue
774+
}
775+
original := raw.(map[string]interface{})
776+
transformed := make(map[string]interface{})
777+
778+
transformedName, err := expandApigeeEnvironmentPropertiesPropertyName(original["name"], d, config)
779+
if err != nil {
780+
return nil, err
781+
} else if val := reflect.ValueOf(transformedName); val.IsValid() && !tpgresource.IsEmptyValue(val) {
782+
transformed["name"] = transformedName
783+
}
784+
785+
transformedValue, err := expandApigeeEnvironmentPropertiesPropertyValue(original["value"], d, config)
786+
if err != nil {
787+
return nil, err
788+
} else if val := reflect.ValueOf(transformedValue); val.IsValid() && !tpgresource.IsEmptyValue(val) {
789+
transformed["value"] = transformedValue
790+
}
791+
792+
req = append(req, transformed)
793+
}
794+
return req, nil
795+
}
796+
797+
func expandApigeeEnvironmentPropertiesPropertyName(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
798+
return v, nil
799+
}
800+
801+
func expandApigeeEnvironmentPropertiesPropertyValue(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
802+
return v, nil
803+
}

google/services/apigee/resource_apigee_environment_generated_test.go

+111
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,117 @@ resource "google_apigee_environment" "apigee_environment" {
243243
`, context)
244244
}
245245

246+
func TestAccApigeeEnvironment_apigeeEnvironmentBasicPropertiesTestExample(t *testing.T) {
247+
acctest.SkipIfVcr(t)
248+
t.Parallel()
249+
250+
context := map[string]interface{}{
251+
"billing_account": envvar.GetTestBillingAccountFromEnv(t),
252+
"org_id": envvar.GetTestOrgFromEnv(t),
253+
"random_suffix": acctest.RandString(t, 10),
254+
}
255+
256+
acctest.VcrTest(t, resource.TestCase{
257+
PreCheck: func() { acctest.AccTestPreCheck(t) },
258+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
259+
ExternalProviders: map[string]resource.ExternalProvider{
260+
"time": {},
261+
},
262+
CheckDestroy: testAccCheckApigeeEnvironmentDestroyProducer(t),
263+
Steps: []resource.TestStep{
264+
{
265+
Config: testAccApigeeEnvironment_apigeeEnvironmentBasicPropertiesTestExample(context),
266+
},
267+
{
268+
ResourceName: "google_apigee_environment.apigee_environment",
269+
ImportState: true,
270+
ImportStateVerify: true,
271+
ImportStateVerifyIgnore: []string{"org_id"},
272+
},
273+
},
274+
})
275+
}
276+
277+
func testAccApigeeEnvironment_apigeeEnvironmentBasicPropertiesTestExample(context map[string]interface{}) string {
278+
return acctest.Nprintf(`
279+
resource "google_project" "project" {
280+
project_id = "tf-test%{random_suffix}"
281+
name = "tf-test%{random_suffix}"
282+
org_id = "%{org_id}"
283+
billing_account = "%{billing_account}"
284+
deletion_policy = "DELETE"
285+
}
286+
287+
resource "time_sleep" "wait_60_seconds" {
288+
create_duration = "60s"
289+
depends_on = [google_project.project]
290+
}
291+
292+
resource "google_project_service" "apigee" {
293+
project = google_project.project.project_id
294+
service = "apigee.googleapis.com"
295+
depends_on = [time_sleep.wait_60_seconds]
296+
}
297+
298+
resource "google_project_service" "servicenetworking" {
299+
project = google_project.project.project_id
300+
service = "servicenetworking.googleapis.com"
301+
depends_on = [google_project_service.apigee]
302+
}
303+
304+
resource "google_project_service" "compute" {
305+
project = google_project.project.project_id
306+
service = "compute.googleapis.com"
307+
depends_on = [google_project_service.servicenetworking]
308+
}
309+
310+
resource "google_compute_network" "apigee_network" {
311+
name = "apigee-network"
312+
project = google_project.project.project_id
313+
depends_on = [google_project_service.compute]
314+
}
315+
316+
resource "google_compute_global_address" "apigee_range" {
317+
name = "apigee-range"
318+
purpose = "VPC_PEERING"
319+
address_type = "INTERNAL"
320+
prefix_length = 16
321+
network = google_compute_network.apigee_network.id
322+
project = google_project.project.project_id
323+
}
324+
325+
resource "google_service_networking_connection" "apigee_vpc_connection" {
326+
network = google_compute_network.apigee_network.id
327+
service = "servicenetworking.googleapis.com"
328+
reserved_peering_ranges = [google_compute_global_address.apigee_range.name]
329+
depends_on = [google_project_service.servicenetworking]
330+
}
331+
332+
resource "google_apigee_organization" "apigee_org" {
333+
analytics_region = "us-central1"
334+
project_id = google_project.project.project_id
335+
authorized_network = google_compute_network.apigee_network.id
336+
depends_on = [
337+
google_service_networking_connection.apigee_vpc_connection,
338+
google_project_service.apigee,
339+
]
340+
}
341+
342+
resource "google_apigee_environment" "apigee_environment" {
343+
org_id = google_apigee_organization.apigee_org.id
344+
name = "tf-test%{random_suffix}"
345+
description = "Apigee Environment"
346+
display_name = "environment-1"
347+
properties {
348+
property {
349+
name = "property-1-key"
350+
value = "property-1-value"
351+
}
352+
}
353+
}
354+
`, context)
355+
}
356+
246357
func testAccCheckApigeeEnvironmentDestroyProducer(t *testing.T) func(s *terraform.State) error {
247358
return func(s *terraform.State) error {
248359
for name, rs := range s.RootModule().Resources {

google/services/apigee/resource_apigee_environment_update_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ resource "google_apigee_environment" "apigee_environment" {
120120
name = "tf-test%{random_suffix}"
121121
description = "Updated Apigee Environment Description"
122122
display_name = "environment-1-updated"
123+
properties {
124+
property {
125+
name = "property-1-key"
126+
value = "property-1-value"
127+
}
128+
}
123129
}
124130
`, context)
125131
}

website/docs/r/apigee_environment.html.markdown

+23
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ The following arguments are supported:
127127
(Optional)
128128
Optional. URI of the forward proxy to be applied to the runtime instances in this environment. Must be in the format of {scheme}://{hostname}:{port}. Note that the scheme must be one of "http" or "https", and the port must be supplied.
129129

130+
* `properties` -
131+
(Optional)
132+
Key-value pairs that may be used for customizing the environment.
133+
Structure is [documented below](#nested_properties).
134+
130135

131136
<a name="nested_node_config"></a>The `node_config` block supports:
132137

@@ -147,6 +152,24 @@ The following arguments are supported:
147152
The current total number of gateway nodes that each environment currently has across
148153
all instances.
149154

155+
<a name="nested_properties"></a>The `properties` block supports:
156+
157+
* `property` -
158+
(Optional)
159+
List of all properties in the object.
160+
Structure is [documented below](#nested_properties_property).
161+
162+
163+
<a name="nested_properties_property"></a>The `property` block supports:
164+
165+
* `name` -
166+
(Optional)
167+
The property key.
168+
169+
* `value` -
170+
(Optional)
171+
The property value.
172+
150173
## Attributes Reference
151174

152175
In addition to the arguments listed above, the following computed attributes are exported:

0 commit comments

Comments
 (0)