Skip to content
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

fix: Stop reading order-related information in resources to fix wrong imports #754

Merged
merged 2 commits into from
Oct 18, 2024
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
22 changes: 12 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ require (
github.com/hashicorp/go-cleanhttp v0.5.2
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/go-version v1.7.0
github.com/hashicorp/terraform-plugin-framework v1.7.0
github.com/hashicorp/terraform-plugin-framework v1.12.0
github.com/hashicorp/terraform-plugin-framework-validators v0.12.0
github.com/hashicorp/terraform-plugin-go v0.23.0
github.com/hashicorp/terraform-plugin-go v0.24.0
github.com/hashicorp/terraform-plugin-log v0.9.0
github.com/hashicorp/terraform-plugin-mux v0.15.0
github.com/hashicorp/terraform-plugin-mux v0.16.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.34.0
github.com/hashicorp/terraform-plugin-testing v1.10.0
github.com/ovh/go-ovh v1.6.0
Expand All @@ -29,7 +29,7 @@ require (
github.com/hashicorp/go-checkpoint v0.5.0 // indirect
github.com/hashicorp/go-hclog v1.6.3 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-plugin v1.6.0 // indirect
github.com/hashicorp/go-plugin v1.6.1 // indirect
github.com/hashicorp/go-retryablehttp v0.7.7 // indirect
github.com/hashicorp/go-uuid v1.0.3 // indirect
github.com/hashicorp/hc-install v0.8.0 // indirect
Expand All @@ -55,19 +55,21 @@ require (
github.com/zclconf/go-cty v1.15.0 // indirect
golang.org/x/crypto v0.26.0 // indirect
golang.org/x/mod v0.19.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/oauth2 v0.18.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.23.0 // indirect
golang.org/x/text v0.17.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de // indirect
google.golang.org/grpc v1.63.2 // indirect
google.golang.org/protobuf v1.34.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240604185151-ef581f913117 // indirect
google.golang.org/grpc v1.66.2 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
)

go 1.21
go 1.22.0

toolchain go1.22.8

retract v0.36.0 // OVH_ENDPOINT environment variable prevent a correct usage of the terraform provider
100 changes: 28 additions & 72 deletions go.sum

Large diffs are not rendered by default.

27 changes: 19 additions & 8 deletions ovh/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
)

var (
reTerminateEmailToken = regexp.MustCompile(`.*https://www.ovh.com/manager/#/billing/confirmTerminate\?id=[[:alnum:]]+&token=([[:alnum:]]+).*`)
terminateEmailMatch = "https://www.ovh.com/manager/#/billing/confirmTerminate"
reTerminateEmailToken = regexp.MustCompile(`.*/billing/confirmTerminate\?id=[[:alnum:]]+&token=([[:alnum:]]+).*`)
terminateEmailMatch = "/billing/confirmTerminate"
)

func genericOrderSchema(withOptions bool) map[string]*schema.Schema {
Expand All @@ -32,9 +32,11 @@ func genericOrderSchema(withOptions bool) map[string]*schema.Schema {

orderSchema := map[string]*schema.Schema{
"ovh_subsidiary": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
ForceNew: true,
Optional: true,
Computed: true,

Description: "Ovh Subsidiary",
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
err := helpers.ValidateSubsidiary(v.(string))
Expand Down Expand Up @@ -64,9 +66,11 @@ func genericOrderSchema(withOptions bool) map[string]*schema.Schema {
},

"plan": {
Type: schema.TypeList,
Required: true,
ForceNew: true,
Type: schema.TypeList,
ForceNew: true,
Optional: true,
Computed: true,

Description: "Product Plan to order",
MaxItems: 1,
Elem: &schema.Resource{
Expand Down Expand Up @@ -239,6 +243,13 @@ func orderCreateFromResource(d *schema.ResourceData, meta interface{}, product s
}

func orderCreate(d *OrderModel, config *Config, product string, waitForCompletion bool) error {
if d.OvhSubsidiary.ValueString() == "" {
return fmt.Errorf("ovh_subsidiary is missing from configuration")
}
if len(d.Plan.Elements()) == 0 {
return fmt.Errorf("plan is missing from configuration")
}

// create Cart
cartParams := &OrderCartCreateOpts{
OvhSubsidiary: strings.ToUpper(d.OvhSubsidiary.ValueString()),
Expand Down
11 changes: 9 additions & 2 deletions ovh/order_resource_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 4 additions & 21 deletions ovh/resource_cloud_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func resourceCloudProjectSchema() map[string]*schema.Schema {

func resourceCloudProjectCreate(d *schema.ResourceData, meta interface{}) error {
if err := orderCreateFromResource(d, meta, "cloud", true); err != nil {
return fmt.Errorf("Could not order cloud project: %q", err)
return fmt.Errorf("could not order cloud project: %q", err)
}

return resourceCloudProjectUpdate(d, meta)
Expand All @@ -92,7 +92,7 @@ func resourceCloudProjectGetServiceName(config *Config, order *MeOrder, details
for _, orderDetail := range details {
operations, err := orderDetailOperations(config.OVHClient, order.OrderId, orderDetail.OrderDetailId)
if err != nil {
return "", fmt.Errorf("Could not read cloudProject order details operations: %q", err)
return "", fmt.Errorf("could not read cloudProject order details operations: %q", err)
}
for _, operation := range operations {
if publicCloudProjectNameFormatRegex.MatchString(operation.Resource.Name) {
Expand All @@ -101,13 +101,13 @@ func resourceCloudProjectGetServiceName(config *Config, order *MeOrder, details
}
}

return "", fmt.Errorf("Unknown service name")
return "", fmt.Errorf("unknown service name")
}

func resourceCloudProjectUpdate(d *schema.ResourceData, meta interface{}) error {
order, details, err := orderReadInResource(d, meta)
if err != nil {
return fmt.Errorf("Could not read cloud project order: %q", err)
return fmt.Errorf("could not read cloud project order: %q", err)
}

config := meta.(*Config)
Expand Down Expand Up @@ -150,23 +150,6 @@ func resourceCloudProjectRead(d *schema.ResourceData, meta interface{}) error {
d.Set(k, v)
}

// Retrieve order information
serviceObj, err := serviceFromServiceName(config.OVHClient, "cloud/project", serviceName)
if err != nil {
return fmt.Errorf("failed to retrieve cloud project details: %w", err)
}
if len(details) > 0 {
serviceObj.OrderDetailExtension = details[0].Extension
}
d.Set("plan", serviceObj.ToSDKv2PlanValue())

// Retrieve subsidiary information
var me MeResponse
if err := config.OVHClient.Get("/me", &me); err != nil {
return fmt.Errorf("error retrieving account information: %w", err)
}
d.Set("ovh_subsidiary", me.OvhSubsidiary)

return nil
}

Expand Down
6 changes: 6 additions & 0 deletions ovh/resource_dedicated_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ func (d *dedicatedServerResource) Schema(ctx context.Context, req resource.Schem
}

func (d *dedicatedServerResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
// Here we force the attribute "plan" to an empty array because it won't be fetched by the Read function.
// If we don't do this, Terraform always shows a diff on the following plans (null => []), due to the
// plan modifier RequiresReplace that initializes the attribute to its zero-value (an empty array).
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("plan"), ovhtypes.TfListNestedValue[PlanValue]{
ListValue: basetypes.NewListValueMust(PlanValue{}.Type(ctx), make([]attr.Value, 0)),
})...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("service_name"), req.ID)...)
}

Expand Down
6 changes: 3 additions & 3 deletions ovh/resource_domain_zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func resourceDomainZoneSchema() map[string]*schema.Schema {

func resourceDomainZoneCreate(d *schema.ResourceData, meta interface{}) error {
if err := orderCreateFromResource(d, meta, "dns", true); err != nil {
return fmt.Errorf("Could not order domain zone: %q", err)
return fmt.Errorf("could not order domain zone: %q", err)
}

return resourceDomainZoneRead(d, meta)
Expand All @@ -80,7 +80,7 @@ func resourceDomainZoneCreate(d *schema.ResourceData, meta interface{}) error {
func resourceDomainZoneRead(d *schema.ResourceData, meta interface{}) error {
_, details, err := orderReadInResource(d, meta)
if err != nil {
return fmt.Errorf("Could not read domainZone order: %q", err)
return fmt.Errorf("could not read domainZone order: %q", err)
}

config := meta.(*Config)
Expand All @@ -104,7 +104,7 @@ func resourceDomainZoneRead(d *schema.ResourceData, meta interface{}) error {
func resourceDomainZoneDelete(d *schema.ResourceData, meta interface{}) error {
_, details, err := orderReadInResource(d, meta)
if err != nil {
return fmt.Errorf("Could not read domainZone order: %q", err)
return fmt.Errorf("could not read domainZone order: %q", err)
}

config := meta.(*Config)
Expand Down
6 changes: 6 additions & 0 deletions ovh/resource_domain_zone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ func TestAccResourceDomainZone_basic(t *testing.T) {
"ovh_domain_zone.zone", "urn"),
),
},
{
ResourceName: "ovh_domain_zone.zone",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"plan", "ovh_subsidiary"},
},
},
})
}
6 changes: 6 additions & 0 deletions ovh/resource_hosting_privatedatabase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ func TestAccHostingPrivateDatabase_basic(t *testing.T) {
"ovh_hosting_privatedatabase.database", "urn"),
),
},
{
ResourceName: "ovh_hosting_privatedatabase.database",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"plan", "ovh_subsidiary"},
},
},
})
}
14 changes: 6 additions & 8 deletions ovh/resource_ip_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ func resourceIpService() *schema.Resource {
Delete: resourceIpServiceDelete,

Importer: &schema.ResourceImporter{
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
State: func(d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) {
d.Set("service_name", d.Id())
return []*schema.ResourceData{d}, nil
},
},
Expand Down Expand Up @@ -107,14 +108,14 @@ func resourceIpServiceCreate(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("failed to get service name from order ID: %w", err)
}

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

return resourceIpServiceUpdate(d, meta)
}

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

serviceName := d.Get("service_name").(string)

log.Printf("[DEBUG] Will update ip: %s", serviceName)
Expand All @@ -136,9 +137,7 @@ func resourceIpServiceRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Will read ip: %s", serviceName)

r := &IpService{}
endpoint := fmt.Sprintf("/ip/service/%s",
url.PathEscape(serviceName),
)
endpoint := fmt.Sprintf("/ip/service/%s", url.PathEscape(serviceName))

// This retry logic is there to handle a known API bug
// which happens while an ipblock is attached/detached from
Expand Down Expand Up @@ -176,11 +175,10 @@ func resourceIpServiceRead(d *schema.ResourceData, meta interface{}) error {

func resourceIpServiceDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
id := d.Id()
serviceName := d.Get("service_name").(string)

terminate := func() (string, error) {
log.Printf("[DEBUG] Will terminate ip %s for order %s", serviceName, id)
log.Printf("[DEBUG] Will terminate ip %s", serviceName)
endpoint := fmt.Sprintf(
"/ip/service/%s/terminate",
url.PathEscape(serviceName),
Expand All @@ -195,7 +193,7 @@ func resourceIpServiceDelete(d *schema.ResourceData, meta interface{}) error {
}

confirmTerminate := func(token string) error {
log.Printf("[DEBUG] Will confirm termination of ip %s for order %s", serviceName, id)
log.Printf("[DEBUG] Will confirm termination of ip %s", serviceName)
endpoint := fmt.Sprintf(
"/ip/service/%s/confirmTermination",
url.PathEscape(serviceName),
Expand Down
6 changes: 6 additions & 0 deletions ovh/resource_ip_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ func TestAccResourceIpService_basic(t *testing.T) {
"ovh_ip_service.ipblock", "description", desc),
),
},
{
ResourceName: "ovh_ip_service.ipblock",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"plan", "ovh_subsidiary"},
},
},
})
}
6 changes: 6 additions & 0 deletions ovh/resource_iploadbalancing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,12 @@ func TestAccResourceIpLoadbalancing_basic(t *testing.T) {
"ovh_iploadbalancing.iplb-lb1", "urn"),
),
},
{
ResourceName: "ovh_iploadbalancing.iplb-lb1",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"plan", "ovh_subsidiary"},
},
},
})
}
Expand Down
5 changes: 1 addition & 4 deletions ovh/resource_okms.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,7 @@ func (r *okmsResource) Update(ctx context.Context, req resource.UpdateRequest, r
data.Iam.DisplayName = planData.DisplayName
err := r.waitKmsUpdate(ctx, data.Id.ValueString(), 1*time.Minute, func(responseData *OkmsModel) bool {
// KMS name was updated successfully
if responseData.DisplayName.ValueString() == data.DisplayName.ValueString() {
return true
}
return false
return responseData.DisplayName.ValueString() == data.DisplayName.ValueString()
})

if err != nil {
Expand Down
Loading