Skip to content

Commit

Permalink
Add support for vpc network configuration in google_service_directory…
Browse files Browse the repository at this point in the history
…_endpoint (#5391) (#3856)

* Servicedirectory: add 'network' attribute to endpoint resource.

* Update description

* Add tests

* Delete main.tf

Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored Nov 16, 2021
1 parent 62c426c commit ea3470c
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/5391.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
serviceDirectory: add support for vpc network configuration in google_service_directory_endpoint.
```
32 changes: 32 additions & 0 deletions google-beta/resource_service_directory_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ up to 512 characters, spread across all key-value pairs.
Metadata that goes beyond any these limits will be rejected.`,
Elem: &schema.Schema{Type: schema.TypeString},
},
"network": {
Type: schema.TypeString,
Optional: true,
Description: `The URL to the network, such as projects/PROJECT_NUMBER/locations/global/networks/NETWORK_NAME.`,
},
"port": {
Type: schema.TypeInt,
Optional: true,
Expand Down Expand Up @@ -117,6 +122,12 @@ func resourceServiceDirectoryEndpointCreate(d *schema.ResourceData, meta interfa
} else if v, ok := d.GetOkExists("metadata"); !isEmptyValue(reflect.ValueOf(metadataProp)) && (ok || !reflect.DeepEqual(v, metadataProp)) {
obj["metadata"] = metadataProp
}
networkProp, err := expandServiceDirectoryEndpointNetwork(d.Get("network"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("network"); !isEmptyValue(reflect.ValueOf(networkProp)) && (ok || !reflect.DeepEqual(v, networkProp)) {
obj["network"] = networkProp
}

url, err := replaceVars(d, config, "{{ServiceDirectoryBasePath}}{{service}}/endpoints?endpointId={{endpoint_id}}")
if err != nil {
Expand Down Expand Up @@ -187,6 +198,9 @@ func resourceServiceDirectoryEndpointRead(d *schema.ResourceData, meta interface
if err := d.Set("metadata", flattenServiceDirectoryEndpointMetadata(res["metadata"], d, config)); err != nil {
return fmt.Errorf("Error reading Endpoint: %s", err)
}
if err := d.Set("network", flattenServiceDirectoryEndpointNetwork(res["network"], d, config)); err != nil {
return fmt.Errorf("Error reading Endpoint: %s", err)
}

return nil
}
Expand Down Expand Up @@ -219,6 +233,12 @@ func resourceServiceDirectoryEndpointUpdate(d *schema.ResourceData, meta interfa
} else if v, ok := d.GetOkExists("metadata"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, metadataProp)) {
obj["metadata"] = metadataProp
}
networkProp, err := expandServiceDirectoryEndpointNetwork(d.Get("network"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("network"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, networkProp)) {
obj["network"] = networkProp
}

url, err := replaceVars(d, config, "{{ServiceDirectoryBasePath}}{{name}}")
if err != nil {
Expand All @@ -239,6 +259,10 @@ func resourceServiceDirectoryEndpointUpdate(d *schema.ResourceData, meta interfa
if d.HasChange("metadata") {
updateMask = append(updateMask, "metadata")
}

if d.HasChange("network") {
updateMask = append(updateMask, "network")
}
// updateMask is a URL parameter but not present in the schema, so replaceVars
// won't set it
url, err = addQueryParams(url, map[string]string{"updateMask": strings.Join(updateMask, ",")})
Expand Down Expand Up @@ -380,6 +404,10 @@ func flattenServiceDirectoryEndpointMetadata(v interface{}, d *schema.ResourceDa
return v
}

func flattenServiceDirectoryEndpointNetwork(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}

func expandServiceDirectoryEndpointAddress(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
Expand All @@ -398,3 +426,7 @@ func expandServiceDirectoryEndpointMetadata(v interface{}, d TerraformResourceDa
}
return m, nil
}

func expandServiceDirectoryEndpointNetwork(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
65 changes: 65 additions & 0 deletions google-beta/resource_service_directory_endpoint_generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,71 @@ resource "google_service_directory_endpoint" "example" {
`, context)
}

func TestAccServiceDirectoryEndpoint_serviceDirectoryEndpointWithNetworkExample(t *testing.T) {
t.Parallel()

context := map[string]interface{}{
"random_suffix": randString(t, 10),
}

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProvidersOiCS,
CheckDestroy: testAccCheckServiceDirectoryEndpointDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccServiceDirectoryEndpoint_serviceDirectoryEndpointWithNetworkExample(context),
},
{
ResourceName: "google_service_directory_endpoint.example",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"service", "endpoint_id"},
},
},
})
}

func testAccServiceDirectoryEndpoint_serviceDirectoryEndpointWithNetworkExample(context map[string]interface{}) string {
return Nprintf(`
data "google_project" "project" {
provider = google-beta
}
resource "google_compute_network" "example" {
provider = google-beta
name = "tf-test-example-network%{random_suffix}"
}
resource "google_service_directory_namespace" "example" {
provider = google-beta
namespace_id = "tf-test-example-namespace%{random_suffix}"
location = "us-central1"
}
resource "google_service_directory_service" "example" {
provider = google-beta
service_id = "tf-test-example-service%{random_suffix}"
namespace = google_service_directory_namespace.example.id
}
resource "google_service_directory_endpoint" "example" {
provider = google-beta
endpoint_id = "tf-test-example-endpoint%{random_suffix}"
service = google_service_directory_service.example.id
metadata = {
stage = "prod"
region = "us-central1"
}
network = "projects/${data.google_project.project.number}/locations/global/networks/${google_compute_network.example.name}"
address = "1.2.3.4"
port = 5353
}
`, context)
}

func testAccCheckServiceDirectoryEndpointDestroyProducer(t *testing.T) func(s *terraform.State) error {
return func(s *terraform.State) error {
for name, rs := range s.RootModule().Resources {
Expand Down
49 changes: 49 additions & 0 deletions website/docs/r/service_directory_endpoint.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,51 @@ resource "google_service_directory_endpoint" "example" {
port = 5353
}
```
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
<a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgit.luolix.top%2Fterraform-google-modules%2Fdocs-examples.git&cloudshell_working_dir=service_directory_endpoint_with_network&cloudshell_image=gcr.io%2Fgraphite-cloud-shell-images%2Fterraform%3Alatest&open_in_editor=main.tf&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md" target="_blank">
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
</a>
</div>
## Example Usage - Service Directory Endpoint With Network


```hcl
data "google_project" "project" {
provider = google-beta
}
resource "google_compute_network" "example" {
provider = google-beta
name = "example-network"
}
resource "google_service_directory_namespace" "example" {
provider = google-beta
namespace_id = "example-namespace"
location = "us-central1"
}
resource "google_service_directory_service" "example" {
provider = google-beta
service_id = "example-service"
namespace = google_service_directory_namespace.example.id
}
resource "google_service_directory_endpoint" "example" {
provider = google-beta
endpoint_id = "example-endpoint"
service = google_service_directory_service.example.id
metadata = {
stage = "prod"
region = "us-central1"
}
network = "projects/${data.google_project.project.number}/locations/global/networks/${google_compute_network.example.name}"
address = "1.2.3.4"
port = 5353
}
```

## Argument Reference

Expand Down Expand Up @@ -103,6 +148,10 @@ The following arguments are supported:
up to 512 characters, spread across all key-value pairs.
Metadata that goes beyond any these limits will be rejected.

* `network` -
(Optional)
The URL to the network, such as projects/PROJECT_NUMBER/locations/global/networks/NETWORK_NAME.


## Attributes Reference

Expand Down

0 comments on commit ea3470c

Please sign in to comment.