Skip to content

Commit

Permalink
enforce CheckDeleted on all resources
Browse files Browse the repository at this point in the history
  • Loading branch information
yann degat committed Dec 2, 2020
1 parent 74403cc commit e0f582a
Show file tree
Hide file tree
Showing 30 changed files with 317 additions and 385 deletions.
144 changes: 141 additions & 3 deletions ovh/data_source_ovh_me_installation_template.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package ovh

import (
"fmt"
"net/url"

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

func dataSourceMeInstallationTemplate() *schema.Resource {
Expand Down Expand Up @@ -254,9 +258,7 @@ func dataSourceMeInstallationTemplate() *schema.Resource {

func dataSourceMeInstallationTemplateRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
name := d.Get("template_name").(string)

template, err := getInstallationTemplate(name, config.OVHClient)
template, err := getInstallationTemplate(d, config.OVHClient)
if err != nil {
return err
}
Expand All @@ -272,6 +274,7 @@ func dataSourceMeInstallationTemplateRead(d *schema.ResourceData, meta interface
return err
}

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

return nil
Expand Down Expand Up @@ -322,3 +325,138 @@ func partialMeInstallationTemplatePartitionSchemesRead(d *schema.ResourceData, m

return nil
}

func getPartitionSchemes(template string, client *ovh.Client) ([]*PartitionScheme, error) {
schemes, err := getPartitionSchemeIds(template, client)
if err != nil {
return nil, err
}

partitionSchemes := []*PartitionScheme{}
for _, scheme := range schemes {
partitionScheme, err := getPartitionScheme(template, scheme, client)
if err != nil {
return nil, err
}

partitionSchemes = append(partitionSchemes, partitionScheme)
}

return partitionSchemes, nil
}

func getPartitionScheme(template, scheme string, client *ovh.Client) (*PartitionScheme, error) {
r := &PartitionScheme{}

endpoint := fmt.Sprintf(
"/me/installationTemplate/%s/partitionScheme/%s",
url.PathEscape(template),
url.PathEscape(scheme),
)

if err := client.Get(endpoint, r); err != nil {
return nil, fmt.Errorf("Error calling GET %s: %s \n", endpoint, err.Error())
}

return r, nil
}

func getPartitionSchemeIds(template string, client *ovh.Client) ([]string, error) {
schemes := []string{}
endpoint := fmt.Sprintf(
"/me/installationTemplate/%s/partitionScheme",
url.PathEscape(template),
)
err := client.Get(endpoint, &schemes)

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

func getPartitionSchemePartitions(template, scheme string, client *ovh.Client) ([]*Partition, error) {
mountPoints := []string{}
endpoint := fmt.Sprintf(
"/me/installationTemplate/%s/partitionScheme/%s/partition",
url.PathEscape(template),
url.PathEscape(scheme),
)
err := client.Get(endpoint, &mountPoints)

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

partitions := []*Partition{}
for _, mountPoint := range mountPoints {
partition, err := getPartitionSchemePartition(template, scheme, mountPoint, client)
if err != nil {
return nil, err
}

partitions = append(partitions, partition)
}

return partitions, nil
}

func getPartitionSchemePartition(template, scheme, mountPoint string, client *ovh.Client) (*Partition, error) {
r := &Partition{}

endpoint := fmt.Sprintf(
"/me/installationTemplate/%s/partitionScheme/%s/partition/%s",
url.PathEscape(template),
url.PathEscape(scheme),
url.PathEscape(mountPoint),
)

if err := client.Get(endpoint, r); err != nil {
return nil, fmt.Errorf("Calling GET %s: %s \n", endpoint, err.Error())
}

return r, nil
}

func getPartitionSchemeHardwareRaids(template, scheme string, client *ovh.Client) ([]*HardwareRaid, error) {
names := []string{}
endpoint := fmt.Sprintf(
"/me/installationTemplate/%s/partitionScheme/%s/hardwareRaid",
url.PathEscape(template),
url.PathEscape(scheme),
)
err := client.Get(endpoint, &names)

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

hardwareRaids := []*HardwareRaid{}
for _, name := range names {
hardwareRaid, err := getPartitionSchemeHardwareRaid(template, scheme, name, client)
if err != nil {
return nil, err
}

hardwareRaids = append(hardwareRaids, hardwareRaid)
}

return hardwareRaids, nil
}

func getPartitionSchemeHardwareRaid(template, scheme, name string, client *ovh.Client) (*HardwareRaid, error) {
r := &HardwareRaid{}

endpoint := fmt.Sprintf(
"/me/installationTemplate/%s/partitionScheme/%s/hardwareRaid/%s",
url.PathEscape(template),
url.PathEscape(scheme),
url.PathEscape(name),
)

if err := client.Get(endpoint, r); err != nil {
return nil, fmt.Errorf("Error calling %s: %s \n", endpoint, err.Error())
}

return r, nil
}
64 changes: 19 additions & 45 deletions ovh/resource_ovh_cloud_project_network_private.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,16 +172,29 @@ func resourceCloudProjectNetworkPrivateRead(d *schema.ResourceData, meta interfa

endpoint := fmt.Sprintf("/cloud/project/%s/network/private/%s", serviceName, d.Id())

err = config.OVHClient.Get(endpoint, r)
if err != nil {
return fmt.Errorf("Error calling %s:\n\t %q", endpoint, err)
if err := config.OVHClient.Get(endpoint, r); err != nil {
return helpers.CheckDeleted(d, err, endpoint)
}

err = readCloudProjectNetworkPrivate(config, d, r)
if err != nil {
return err
d.Set("name", r.Name)
d.Set("status", r.Status)
d.Set("type", r.Type)
d.Set("vlan_id", r.Vlanid)

regions_status := make([]map[string]interface{}, 0)
regions := make([]string, 0)

for i := range r.Regions {
region := make(map[string]interface{})
region["region"] = r.Regions[i].Region
region["status"] = r.Regions[i].Status
regions_status = append(regions_status, region)
regions = append(regions, fmt.Sprintf(r.Regions[i].Region))
}
d.Set("regions_status", regions_status)
d.Set("regions", regions)

d.SetId(r.Id)
d.Set("service_name", serviceName)
d.Set("project_id", serviceName)

Expand Down Expand Up @@ -251,45 +264,6 @@ func resourceCloudProjectNetworkPrivateDelete(d *schema.ResourceData, meta inter
return nil
}

func readCloudProjectNetworkPrivate(config *Config, d *schema.ResourceData, r *CloudProjectNetworkPrivateResponse) error {
d.Set("name", r.Name)
d.Set("status", r.Status)
d.Set("type", r.Type)
d.Set("vlan_id", r.Vlanid)

regions_status := make([]map[string]interface{}, 0)
regions := make([]string, 0)

for i := range r.Regions {
region := make(map[string]interface{})
region["region"] = r.Regions[i].Region
region["status"] = r.Regions[i].Status
regions_status = append(regions_status, region)
regions = append(regions, fmt.Sprintf(r.Regions[i].Region))
}
d.Set("regions_status", regions_status)
d.Set("regions", regions)

d.SetId(r.Id)
return nil
}

func cloudNetworkPrivateExists(serviceName, id string, c *ovh.Client) error {
r := &CloudProjectNetworkPrivateResponse{}

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

endpoint := fmt.Sprintf("/cloud/project/%s/network/private/%s", serviceName, id)

err := c.Get(endpoint, r)
if err != nil {
return fmt.Errorf("calling %s:\n\t %q", endpoint, err)
}
log.Printf("[DEBUG] Read public cloud private network: %s", r)

return nil
}

// AttachmentStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
// an Attachment Task.
func waitForCloudProjectNetworkPrivateActive(c *ovh.Client, serviceName, CloudProjectNetworkPrivateId string) resource.StateRefreshFunc {
Expand Down
114 changes: 40 additions & 74 deletions ovh/resource_ovh_cloud_project_network_private_subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (

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

"github.com/ovh/go-ovh/ovh"
)

func resourceOvhCloudProjectNetworkPrivateSubnetImportState(
Expand Down Expand Up @@ -192,89 +190,26 @@ func resourceCloudProjectNetworkPrivateSubnetRead(d *schema.ResourceData, meta i

networkId := d.Get("network_id").(string)

r := []*CloudProjectNetworkPrivatesResponse{}
subnets := []*CloudProjectNetworkPrivatesResponse{}

log.Printf("[DEBUG] Will read public cloud private network subnet for project: %s, network: %s, id: %s", serviceName, networkId, d.Id())

endpoint := fmt.Sprintf("/cloud/project/%s/network/private/%s/subnet", serviceName, networkId)

err = config.OVHClient.Get(endpoint, &r)
if err != nil {
return fmt.Errorf("calling GET %s:\n\t %q", endpoint, err)
}

err = readCloudProjectNetworkPrivateSubnet(d, r)
if err != nil {
return err
}

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

log.Printf("[DEBUG] Read Public Cloud Private Network %v", r)
return nil
}

func resourceCloudProjectNetworkPrivateSubnetDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

serviceName, err := helpers.GetCloudProjectServiceName(d)
if err != nil {
return err
if err := config.OVHClient.Get(endpoint, &subnets); err != nil {
return helpers.CheckDeleted(d, err, endpoint)
}

networkId := d.Get("network_id").(string)
id := d.Id()

log.Printf("[DEBUG] Will delete public cloud private network subnet for project: %s, network: %s, id: %s", serviceName, networkId, id)

endpoint := fmt.Sprintf("/cloud/project/%s/network/private/%s/subnet/%s", serviceName, id, id)

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

d.SetId("")

log.Printf("[DEBUG] Deleted Public Cloud %s Private Network %s Subnet %s", serviceName, networkId, id)
return nil
}

func cloudNetworkPrivateSubnetExists(serviceName, networkId, id string, c *ovh.Client) error {
r := []*CloudProjectNetworkPrivatesResponse{}

log.Printf("[DEBUG] Will read public cloud private network subnet for project: %s, network: %s, id: %s", serviceName, networkId, id)

endpoint := fmt.Sprintf("/cloud/project/%s/network/private/%s/subnet", serviceName, networkId)

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

s := findCloudProjectNetworkPrivateSubnet(r, id)
if s == nil {
return fmt.Errorf("Subnet %s doesn't exists for project %s and network %s", id, serviceName, networkId)
}

return nil
}

func findCloudProjectNetworkPrivateSubnet(rs []*CloudProjectNetworkPrivatesResponse, id string) *CloudProjectNetworkPrivatesResponse {
for i := range rs {
if rs[i].Id == id {
return rs[i]
var r *CloudProjectNetworkPrivatesResponse
for i := range subnets {
if subnets[i].Id == d.Id() {
r = subnets[i]
}
}

return nil
}

func readCloudProjectNetworkPrivateSubnet(d *schema.ResourceData, rs []*CloudProjectNetworkPrivatesResponse) error {
r := findCloudProjectNetworkPrivateSubnet(rs, d.Id())
if r == nil {
return fmt.Errorf("%s subnet not found", d.Id())
d.SetId("")
return nil
}

d.Set("gateway_ip", r.GatewayIp)
Expand Down Expand Up @@ -305,6 +240,37 @@ func readCloudProjectNetworkPrivateSubnet(d *schema.ResourceData, rs []*CloudPro
}

d.SetId(r.Id)

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

log.Printf("[DEBUG] Read Public Cloud Private Network %v", r)
return nil
}

func resourceCloudProjectNetworkPrivateSubnetDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

serviceName, err := helpers.GetCloudProjectServiceName(d)
if err != nil {
return err
}

networkId := d.Get("network_id").(string)
id := d.Id()

log.Printf("[DEBUG] Will delete public cloud private network subnet for project: %s, network: %s, id: %s", serviceName, networkId, id)

endpoint := fmt.Sprintf("/cloud/project/%s/network/private/%s/subnet/%s", serviceName, id, id)

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

d.SetId("")

log.Printf("[DEBUG] Deleted Public Cloud %s Private Network %s Subnet %s", serviceName, networkId, id)
return nil
}

Expand Down
Loading

0 comments on commit e0f582a

Please sign in to comment.