Skip to content

d|r/cloud*: use service_name for identifier #173

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

Merged
merged 1 commit into from
Nov 30, 2020
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
44 changes: 31 additions & 13 deletions ovh/data_source_ovh_cloud_region.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,32 @@ import (
"net/url"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/ovh/terraform-provider-ovh/ovh/helpers/hashcode"

"github.com/ovh/go-ovh/ovh"
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
"github.com/ovh/terraform-provider-ovh/ovh/helpers/hashcode"
)

func dataSourceCloudRegion() *schema.Resource {
return &schema.Resource{
Read: dataSourceCloudRegionRead,
Schema: map[string]*schema.Schema{
"project_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
DefaultFunc: schema.EnvDefaultFunc("OVH_PROJECT_ID", nil),
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
DefaultFunc: schema.EnvDefaultFunc("OVH_PROJECT_ID", nil),
Description: "Id of the cloud project. DEPRECATED, use `service_name` instead",
ConflictsWith: []string{"service_name"},
},
"service_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
DefaultFunc: schema.EnvDefaultFunc("OVH_CLOUD_PROJECT_SERVICE", nil),
Description: "Service name of the resource representing the id of the cloud project.",
ConflictsWith: []string{"project_id"},
},
"name": {
Type: schema.TypeString,
Expand Down Expand Up @@ -70,12 +82,16 @@ func dataSourceCloudRegion() *schema.Resource {

func dataSourceCloudRegionRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
projectId := d.Get("project_id").(string)
serviceName, err := helpers.GetCloudProjectServiceName(d)
if err != nil {
return err
}

name := d.Get("name").(string)

log.Printf("[DEBUG] Will read public cloud region %s for project: %s", name, projectId)
log.Printf("[DEBUG] Will read public cloud region %s for project: %s", name, serviceName)

region, err := getCloudRegion(projectId, name, config.OVHClient)
region, err := getCloudRegion(serviceName, name, config.OVHClient)
if err != nil {
return err
}
Expand All @@ -98,18 +114,20 @@ func dataSourceCloudRegionRead(d *schema.ResourceData, meta interface{}) error {
}

d.Set("services", services)
d.SetId(fmt.Sprintf("%s_%s", projectId, name))
d.Set("service_name", serviceName)
d.Set("project_id", serviceName)
d.SetId(fmt.Sprintf("%s_%s", serviceName, name))

return nil
}

func getCloudRegion(projectId, region string, client *ovh.Client) (*CloudRegionResponse, error) {
log.Printf("[DEBUG] Will read public cloud region %s for project: %s", region, projectId)
func getCloudRegion(serviceName, region string, client *ovh.Client) (*CloudRegionResponse, error) {
log.Printf("[DEBUG] Will read public cloud region %s for project: %s", region, serviceName)

response := &CloudRegionResponse{}
endpoint := fmt.Sprintf(
"/cloud/project/%s/region/%s",
url.PathEscape(projectId),
url.PathEscape(serviceName),
url.PathEscape(region),
)
err := client.Get(endpoint, response)
Expand Down
40 changes: 36 additions & 4 deletions ovh/data_source_ovh_cloud_region_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,46 @@ func TestAccCloudRegionDataSource_basic(t *testing.T) {
})
}

func TestAccCloudRegionDataSourceDeprecated_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheckCloud(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCloudRegionDatasourceDeprecatedConfig,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("data.ovh_cloud_region.region_attr.0", "name"),
resource.TestCheckResourceAttrSet("data.ovh_cloud_region.region_attr.0", "services.#"),
resource.TestCheckResourceAttrSet("data.ovh_cloud_region.region_attr.1", "name"),
resource.TestCheckResourceAttrSet("data.ovh_cloud_region.region_attr.1", "services.#"),
resource.TestCheckResourceAttrSet("data.ovh_cloud_region.region_attr.2", "name"),
resource.TestCheckResourceAttrSet("data.ovh_cloud_region.region_attr.2", "services.#"),
),
},
},
})
}

var testAccCloudRegionDatasourceConfig = fmt.Sprintf(`
data "ovh_cloud_regions" "regions" {
service_name = "%s"
}

data "ovh_cloud_region" "region_attr" {
count = 3
service_name = data.ovh_cloud_regions.regions.service_name
name = element(sort(data.ovh_cloud_regions.regions.names), count.index)
}
`, os.Getenv("OVH_CLOUD_PROJECT_SERVICE_TEST"))

var testAccCloudRegionDatasourceDeprecatedConfig = fmt.Sprintf(`
data "ovh_cloud_regions" "regions" {
project_id = "%s"
}

data "ovh_cloud_region" "region_attr" {
count = 3
project_id = data.ovh_cloud_regions.regions.project_id
name = element(sort(data.ovh_cloud_regions.regions.names), count.index)
count = 3
project_id = data.ovh_cloud_regions.regions.service_name
name = element(sort(data.ovh_cloud_regions.regions.names), count.index)
}
`, os.Getenv("OVH_PUBLIC_CLOUD"))
`, os.Getenv("OVH_CLOUD_PROJECT_SERVICE_TEST"))
43 changes: 30 additions & 13 deletions ovh/data_source_ovh_cloud_regions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,35 @@ import (
"net/url"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
)

func dataSourceCloudRegions() *schema.Resource {
return &schema.Resource{
Read: dataSourceCloudRegionsRead,
Schema: map[string]*schema.Schema{
"project_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
DefaultFunc: schema.EnvDefaultFunc("OVH_PROJECT_ID", nil),
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
DefaultFunc: schema.EnvDefaultFunc("OVH_PROJECT_ID", nil),
Description: "Id of the cloud project. DEPRECATED, use `service_name` instead",
ConflictsWith: []string{"service_name"},
},
"service_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
DefaultFunc: schema.EnvDefaultFunc("OVH_CLOUD_PROJECT_SERVICE", nil),
Description: "Service name of the resource representing the id of the cloud project.",
ConflictsWith: []string{"project_id"},
},
"has_services_up": {
Type: schema.TypeSet,
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
"names": {
Type: schema.TypeSet,
Expand All @@ -36,27 +48,32 @@ func dataSourceCloudRegions() *schema.Resource {

func dataSourceCloudRegionsRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
projectId := d.Get("project_id").(string)
serviceName, err := helpers.GetCloudProjectServiceName(d)
if err != nil {
return err
}

log.Printf("[DEBUG] Will read public cloud regions for project: %s", projectId)
log.Printf("[DEBUG] Will read public cloud regions for project: %s", serviceName)

endpoint := fmt.Sprintf(
"/cloud/project/%s/region",
url.PathEscape(projectId),
url.PathEscape(serviceName),
)

names := make([]string, 0)
err := config.OVHClient.Get(endpoint, &names)
err = config.OVHClient.Get(endpoint, &names)

if err != nil {
return fmt.Errorf("Error calling %s:\n\t %q", endpoint, err)
}

d.SetId(projectId)
d.SetId(serviceName)
d.Set("service_name", serviceName)
d.Set("project_id", serviceName)

var services []interface{}
if servicesVal, ok := d.GetOk("has_services_up"); ok {
services = servicesVal.(*schema.Set).List()
services = servicesVal.([]interface{})
}

// no filtering on services
Expand All @@ -67,7 +84,7 @@ func dataSourceCloudRegionsRead(d *schema.ResourceData, meta interface{}) error

filtered_names := make([]string, 0)
for _, n := range names {
region, err := getCloudRegion(projectId, n, config.OVHClient)
region, err := getCloudRegion(serviceName, n, config.OVHClient)
if err != nil {
return err
}
Expand Down
26 changes: 13 additions & 13 deletions ovh/data_source_ovh_cloud_regions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,26 @@ func TestAccCloudRegionsDataSource_basic(t *testing.T) {
})
}

func TestAccCloudRegionsDataSource_withNetworkUp(t *testing.T) {
func TestAccCloudRegionsDeprecatedDataSource_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheckCloud(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCloudRegionsDatasourceConfig_withNetwork,
Config: testAccCloudRegionsDatasourceDeprecatedConfig,
Check: resource.TestCheckResourceAttrSet("data.ovh_cloud_regions.regions", "names.#"),
},
},
})
}

func TestAccCloudRegionsDataSource_withProjectIdEnvVar(t *testing.T) {
os.Setenv("OVH_PROJECT_ID", os.Getenv("OVH_PUBLIC_CLOUD"))

func TestAccCloudRegionsDataSource_withNetworkUp(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheckCloud(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCloudRegionsDatasourceConfig_withProjectIdEnvVar,
Config: testAccCloudRegionsDatasourceConfig_withNetwork,
Check: resource.TestCheckResourceAttrSet("data.ovh_cloud_regions.regions", "names.#"),
},
},
Expand All @@ -51,17 +49,19 @@ func TestAccCloudRegionsDataSource_withProjectIdEnvVar(t *testing.T) {

var testAccCloudRegionsDatasourceConfig = fmt.Sprintf(`
data "ovh_cloud_regions" "regions" {
project_id = "%s"
service_name = "%s"
}
`, os.Getenv("OVH_PUBLIC_CLOUD"))
`, os.Getenv("OVH_CLOUD_PROJECT_SERVICE_TEST"))

var testAccCloudRegionsDatasourceConfig_withProjectIdEnvVar = `
data "ovh_cloud_regions" "regions" {}
`
var testAccCloudRegionsDatasourceDeprecatedConfig = fmt.Sprintf(`
data "ovh_cloud_regions" "regions" {
project_id = "%s"
}
`, os.Getenv("OVH_CLOUD_PROJECT_SERVICE_TEST"))

var testAccCloudRegionsDatasourceConfig_withNetwork = fmt.Sprintf(`
data "ovh_cloud_regions" "regions" {
project_id = "%s"
service_name = "%s"
has_services_up = ["network"]
}
`, os.Getenv("OVH_PUBLIC_CLOUD"))
`, os.Getenv("OVH_CLOUD_PROJECT_SERVICE_TEST"))
2 changes: 1 addition & 1 deletion ovh/data_source_ovh_domain_zone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func TestAccDomainZoneDataSource_basic(t *testing.T) {
zoneName := os.Getenv("OVH_ZONE")
zoneName := os.Getenv("OVH_ZONE_TEST")
config := fmt.Sprintf(testAccDomainZoneDatasourceConfig_Basic, zoneName)

resource.Test(t, resource.TestCase{
Expand Down
4 changes: 2 additions & 2 deletions ovh/data_source_ovh_iploadbalancing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func TestAccIpLoadbalancingDataSource_basic(t *testing.T) {
serviceName := os.Getenv("OVH_IPLB_SERVICE")
serviceName := os.Getenv("OVH_IPLB_SERVICE_TEST")
config := fmt.Sprintf(testAccIpLoadbalancingDatasourceConfig_Basic, serviceName)

resource.Test(t, resource.TestCase{
Expand All @@ -30,7 +30,7 @@ func TestAccIpLoadbalancingDataSource_basic(t *testing.T) {
}

func TestAccIpLoadbalancingDataSource_statevrack(t *testing.T) {
serviceName := os.Getenv("OVH_IPLB_SERVICE")
serviceName := os.Getenv("OVH_IPLB_SERVICE_TEST")
config := fmt.Sprintf(testAccIpLoadbalancingDatasourceConfig_StateAndVrack, serviceName)

resource.Test(t, resource.TestCase{
Expand Down
4 changes: 2 additions & 2 deletions ovh/data_source_ovh_iploadbalancing_vrack_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ data ovh_iploadbalancing_vrack_network "network" {
vrack_network_id = ovh_iploadbalancing_vrack_network.network.vrack_network_id
}
`,
os.Getenv("OVH_IPLB_SERVICE"),
os.Getenv("OVH_VRACK"),
os.Getenv("OVH_IPLB_SERVICE_TEST"),
os.Getenv("OVH_VRACK_SERVICE_TEST"),
testAccIpLoadbalancingVrackNetworkSubnet,
testAccIpLoadbalancingVrackNetworkVlan1001,
testAccIpLoadbalancingVrackNetworkNatIp,
Expand Down
4 changes: 2 additions & 2 deletions ovh/data_source_ovh_iploadbalancing_vrack_networks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ data ovh_iploadbalancing_vrack_networks "networks" {
service_name = data.ovh_iploadbalancing.iplb.service_name
subnet = "10.0.0.0/24"
}
`, os.Getenv("OVH_IPLB_SERVICE"))
`, os.Getenv("OVH_IPLB_SERVICE_TEST"))

var testAccIpLoadbalancingVrackNetworksDatasourceConfig_withFilters = fmt.Sprintf(`
data ovh_iploadbalancing "iplb" {
Expand All @@ -61,4 +61,4 @@ data ovh_iploadbalancing_vrack_networks "networks" {
subnet = "10.0.0.0/24"
vlan_id = 0
}
`, os.Getenv("OVH_IPLB_SERVICE"))
`, os.Getenv("OVH_IPLB_SERVICE_TEST"))
34 changes: 34 additions & 0 deletions ovh/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,3 +323,37 @@ func StringsFromSchema(d *schema.ResourceData, id string) ([]string, error) {
}
return xs, nil
}

// This function shall be removed as soon as the deprecated project_id
// attribute is removed from schemas
func GetCloudProjectServiceName(d *schema.ResourceData) (string, error) {
projectId := GetNilStringPointerFromData(d, "project_id")
serviceNamePtr := GetNilStringPointerFromData(d, "service_name")

if serviceNamePtr == nil && projectId != nil && *projectId != "" {
serviceNamePtr = projectId
}

if serviceNamePtr == nil || *serviceNamePtr == "" {
return "", fmt.Errorf("service_name attribute is mandatory.")
}

return *serviceNamePtr, nil
}

// This function shall be removed as soon as the deprecated vrack_id
// attribute is removed from schemas
func GetVrackServiceName(d *schema.ResourceData) (string, error) {
vrackId := GetNilStringPointerFromData(d, "vrack_id")
serviceNamePtr := GetNilStringPointerFromData(d, "service_name")

if serviceNamePtr == nil && vrackId != nil && *vrackId != "" {
serviceNamePtr = vrackId
}

if serviceNamePtr == nil || *serviceNamePtr == "" {
return "", fmt.Errorf("service_name attribute is mandatory.")
}

return *serviceNamePtr, nil
}
4 changes: 2 additions & 2 deletions ovh/import_cloud_network_private_subnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestAccCloudNetworkPrivateSubnet_importBasic(t *testing.T) {
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCloudNetworkPrivateSubnetConfig(),
Config: testAccCloudNetworkPrivateSubnetConfig(testAccCloudNetworkPrivateSubnetConfig_basic),
},
{
ResourceName: "ovh_cloud_network_private_subnet.subnet",
Expand All @@ -38,7 +38,7 @@ func testAccCloudNetworkPrivateSubnetImportId(resourceName string) resource.Impo

return fmt.Sprintf(
"%s/%s/%s",
subnet.Primary.Attributes["project_id"],
subnet.Primary.Attributes["service_name"],
subnet.Primary.Attributes["network_id"],
subnet.Primary.ID,
), nil
Expand Down
2 changes: 1 addition & 1 deletion ovh/import_iploadbalancing_http_farm_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,4 @@ func testAccIpLoadbalancingHttpFarmServerImportId(resourceName string) resource.
// ping.ovh.net ip is used for test purposes
var httpServerAddress = "198.27.92.1"
var testAccIpLoadbalancingHttpFarmServerConfig_basic = fmt.Sprintf(testAccIpLoadbalancingHttpFarmServerConfig,
os.Getenv("OVH_IPLB_SERVICE"), "testfarm", 12345, "all", "testserver", httpServerAddress)
os.Getenv("OVH_IPLB_SERVICE_TEST"), "testfarm", 12345, "all", "testserver", httpServerAddress)
Loading