mirror of
https://github.com/letic/terraform-provider-google.git
synced 2024-07-03 08:42:39 +00:00
provider/google: datasource subnetwork and network (#12442)
* first version of this datasource * add network and subnetwork datasource and documentation * modify sidebar reference in documentation * fix elements after review on network and subnetwork datasources * fix fmt on Google provider.go * modify code with the review * modify documentation layout order * fix alphabetic order in provider.go * fix rebase issue and documentation datasource => data
This commit is contained in:
parent
3e8a5d272e
commit
aa913e6dee
73
data_source_google_compute_network.go
Normal file
73
data_source_google_compute_network.go
Normal file
|
@ -0,0 +1,73 @@
|
|||
package google
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/terraform/helper/schema"
|
||||
"google.golang.org/api/googleapi"
|
||||
)
|
||||
|
||||
func dataSourceGoogleComputeNetwork() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
Read: dataSourceGoogleComputeNetworkRead,
|
||||
|
||||
Schema: map[string]*schema.Schema{
|
||||
"name": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
},
|
||||
|
||||
"description": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
},
|
||||
|
||||
"gateway_ipv4": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
},
|
||||
|
||||
"self_link": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
},
|
||||
|
||||
"project": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
},
|
||||
|
||||
"subnetworks_self_links": {
|
||||
Type: schema.TypeList,
|
||||
Computed: true,
|
||||
Elem: &schema.Schema{Type: schema.TypeString},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func dataSourceGoogleComputeNetworkRead(d *schema.ResourceData, meta interface{}) error {
|
||||
config := meta.(*Config)
|
||||
|
||||
project, err := getProject(d, config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
network, err := config.clientCompute.Networks.Get(
|
||||
project, d.Get("name").(string)).Do()
|
||||
if err != nil {
|
||||
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
|
||||
// The resource doesn't exist anymore
|
||||
|
||||
return fmt.Errorf("Network Not Found : %s", d.Get("name"))
|
||||
}
|
||||
|
||||
return fmt.Errorf("Error reading network: %s", err)
|
||||
}
|
||||
d.Set("gateway_ipv4", network.GatewayIPv4)
|
||||
d.Set("self_link", network.SelfLink)
|
||||
d.Set("description", network.Description)
|
||||
d.Set("subnetworks_self_links", network.Subnetworks)
|
||||
d.SetId(network.Name)
|
||||
return nil
|
||||
}
|
68
data_source_google_compute_network_test.go
Normal file
68
data_source_google_compute_network_test.go
Normal file
|
@ -0,0 +1,68 @@
|
|||
package google
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/hashicorp/terraform/helper/resource"
|
||||
"github.com/hashicorp/terraform/terraform"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAccDataSourceGoogleNetwork(t *testing.T) {
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
Providers: testAccProviders,
|
||||
Steps: []resource.TestStep{
|
||||
resource.TestStep{
|
||||
Config: TestAccDataSourceGoogleNetworkConfig,
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testAccDataSourceGoogleNetworkCheck("data.google_compute_network.my_network", "google_compute_network.foobar"),
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func testAccDataSourceGoogleNetworkCheck(data_source_name string, resource_name string) resource.TestCheckFunc {
|
||||
return func(s *terraform.State) error {
|
||||
ds, ok := s.RootModule().Resources[data_source_name]
|
||||
if !ok {
|
||||
return fmt.Errorf("root module has no resource called %s", data_source_name)
|
||||
}
|
||||
|
||||
rs, ok := s.RootModule().Resources[resource_name]
|
||||
if !ok {
|
||||
return fmt.Errorf("can't find %s in state", resource_name)
|
||||
}
|
||||
|
||||
ds_attr := ds.Primary.Attributes
|
||||
rs_attr := rs.Primary.Attributes
|
||||
network_attrs_to_test := []string{
|
||||
"id",
|
||||
"self_link",
|
||||
"name",
|
||||
"description",
|
||||
}
|
||||
|
||||
for _, attr_to_check := range network_attrs_to_test {
|
||||
if ds_attr[attr_to_check] != rs_attr[attr_to_check] {
|
||||
return fmt.Errorf(
|
||||
"%s is %s; want %s",
|
||||
attr_to_check,
|
||||
ds_attr[attr_to_check],
|
||||
rs_attr[attr_to_check],
|
||||
)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
var TestAccDataSourceGoogleNetworkConfig = `
|
||||
resource "google_compute_network" "foobar" {
|
||||
name = "network-test"
|
||||
description = "my-description"
|
||||
}
|
||||
|
||||
data "google_compute_network" "my_network" {
|
||||
name = "${google_compute_network.foobar.name}"
|
||||
}`
|
87
data_source_google_compute_subnetwork.go
Normal file
87
data_source_google_compute_subnetwork.go
Normal file
|
@ -0,0 +1,87 @@
|
|||
package google
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/terraform/helper/schema"
|
||||
"google.golang.org/api/googleapi"
|
||||
)
|
||||
|
||||
func dataSourceGoogleComputeSubnetwork() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
Read: dataSourceGoogleComputeSubnetworkRead,
|
||||
|
||||
Schema: map[string]*schema.Schema{
|
||||
"name": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
},
|
||||
|
||||
"description": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
},
|
||||
"self_link": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
},
|
||||
"ip_cidr_range": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
},
|
||||
"network": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
},
|
||||
"gateway_address": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
},
|
||||
"region": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
},
|
||||
|
||||
"project": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func dataSourceGoogleComputeSubnetworkRead(d *schema.ResourceData, meta interface{}) error {
|
||||
config := meta.(*Config)
|
||||
|
||||
project, err := getProject(d, config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
region, err := getRegion(d, config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
subnetwork, err := config.clientCompute.Subnetworks.Get(
|
||||
project, region, d.Get("name").(string)).Do()
|
||||
if err != nil {
|
||||
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
|
||||
// The resource doesn't exist anymore
|
||||
|
||||
return fmt.Errorf("Subnetwork Not Found")
|
||||
}
|
||||
|
||||
return fmt.Errorf("Error reading Subnetwork: %s", err)
|
||||
}
|
||||
|
||||
d.Set("ip_cidr_range", subnetwork.IpCidrRange)
|
||||
d.Set("self_link", subnetwork.SelfLink)
|
||||
d.Set("description", subnetwork.Description)
|
||||
d.Set("gateway_address", subnetwork.GatewayAddress)
|
||||
d.Set("network", subnetwork.Network)
|
||||
|
||||
//Subnet id creation is defined in resource_compute_subnetwork.go
|
||||
subnetwork.Region = region
|
||||
d.SetId(createSubnetID(subnetwork))
|
||||
return nil
|
||||
}
|
81
data_source_google_compute_subnetwork_test.go
Normal file
81
data_source_google_compute_subnetwork_test.go
Normal file
|
@ -0,0 +1,81 @@
|
|||
package google
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform/helper/resource"
|
||||
"github.com/hashicorp/terraform/terraform"
|
||||
)
|
||||
|
||||
func TestAccDataSourceGoogleSubnetwork(t *testing.T) {
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
Providers: testAccProviders,
|
||||
Steps: []resource.TestStep{
|
||||
resource.TestStep{
|
||||
Config: TestAccDataSourceGoogleSubnetworkConfig,
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testAccDataSourceGoogleSubnetworkCheck("data.google_compute_subnetwork.my_subnetwork", "google_compute_subnetwork.foobar"),
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func testAccDataSourceGoogleSubnetworkCheck(data_source_name string, resource_name string) resource.TestCheckFunc {
|
||||
return func(s *terraform.State) error {
|
||||
ds, ok := s.RootModule().Resources[data_source_name]
|
||||
if !ok {
|
||||
return fmt.Errorf("root module has no resource called %s", data_source_name)
|
||||
}
|
||||
|
||||
rs, ok := s.RootModule().Resources[resource_name]
|
||||
if !ok {
|
||||
return fmt.Errorf("can't find %s in state", resource_name)
|
||||
}
|
||||
|
||||
ds_attr := ds.Primary.Attributes
|
||||
rs_attr := rs.Primary.Attributes
|
||||
|
||||
subnetwork_attrs_to_test := []string{
|
||||
"id",
|
||||
"self_link",
|
||||
"name",
|
||||
"description",
|
||||
"ip_cidr_range",
|
||||
"network",
|
||||
}
|
||||
|
||||
for _, attr_to_check := range subnetwork_attrs_to_test {
|
||||
if ds_attr[attr_to_check] != rs_attr[attr_to_check] {
|
||||
return fmt.Errorf(
|
||||
"%s is %s; want %s",
|
||||
attr_to_check,
|
||||
ds_attr[attr_to_check],
|
||||
rs_attr[attr_to_check],
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
var TestAccDataSourceGoogleSubnetworkConfig = `
|
||||
|
||||
resource "google_compute_network" "foobar" {
|
||||
name = "network-test"
|
||||
description = "my-description"
|
||||
}
|
||||
resource "google_compute_subnetwork" "foobar" {
|
||||
name = "subnetwork-test"
|
||||
description = "my-description"
|
||||
ip_cidr_range = "10.0.0.0/24"
|
||||
network = "${google_compute_network.foobar.self_link}"
|
||||
}
|
||||
|
||||
data "google_compute_subnetwork" "my_subnetwork" {
|
||||
name = "${google_compute_subnetwork.foobar.name}"
|
||||
}
|
||||
`
|
|
@ -48,8 +48,10 @@ func Provider() terraform.ResourceProvider {
|
|||
},
|
||||
|
||||
DataSourcesMap: map[string]*schema.Resource{
|
||||
"google_iam_policy": dataSourceGoogleIamPolicy(),
|
||||
"google_compute_zones": dataSourceGoogleComputeZones(),
|
||||
"google_compute_network": dataSourceGoogleComputeNetwork(),
|
||||
"google_compute_subnetwork": dataSourceGoogleComputeSubnetwork(),
|
||||
"google_compute_zones": dataSourceGoogleComputeZones(),
|
||||
"google_iam_policy": dataSourceGoogleIamPolicy(),
|
||||
},
|
||||
|
||||
ResourcesMap: map[string]*schema.Resource{
|
||||
|
|
Loading…
Reference in New Issue
Block a user