Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
outscale-toa committed Jan 29, 2024
1 parent e4751de commit 472cadf
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 172 deletions.
5 changes: 0 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ require (
github.com/hashicorp/terraform v0.14.0
github.com/hashicorp/terraform-plugin-sdk v1.17.2
github.com/nav-inc/datetime v0.1.3
github.com/openlyinc/pointy v1.1.2
github.com/outscale/osc-sdk-go/v2 v2.18.2
github.com/spf13/cast v1.3.1
)
Expand Down Expand Up @@ -60,7 +59,6 @@ require (
github.com/huandu/xstrings v1.3.2 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/jstemmer/go-junit-report v0.9.1 // indirect
github.com/klauspost/compress v1.17.1 // indirect
github.com/mattn/go-colorable v0.1.1 // indirect
github.com/mattn/go-isatty v0.0.5 // indirect
Expand All @@ -82,14 +80,11 @@ require (
github.com/zclconf/go-cty-yaml v1.0.2 // indirect
go.opencensus.io v0.24.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/oauth2 v0.13.0 // indirect
golang.org/x/sync v0.4.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/tools v0.6.0 // indirect
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect
google.golang.org/api v0.147.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
Expand Down
37 changes: 4 additions & 33 deletions go.sum

Large diffs are not rendered by default.

37 changes: 5 additions & 32 deletions outscale/data_source_outscale_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,13 +291,11 @@ func getOApiVMAttributesSchema() map[string]*schema.Schema {
},
"block_device_mappings_created": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"bsu": {
Type: schema.TypeMap,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand All @@ -322,14 +320,13 @@ func getOApiVMAttributesSchema() map[string]*schema.Schema {
},
"device_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
},
},
"bsu_optimized": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
"client_token": {
Expand All @@ -342,7 +339,6 @@ func getOApiVMAttributesSchema() map[string]*schema.Schema {
},
"deletion_protection": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
"hypervisor": {
Expand All @@ -351,28 +347,24 @@ func getOApiVMAttributesSchema() map[string]*schema.Schema {
},
"image_id": {
Type: schema.TypeString,
ForceNew: true,
Optional: true,
Computed: true,
},
"is_source_dest_checked": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
"keypair_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"security_group_ids": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"security_group_names": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"launch_number": {
Expand Down Expand Up @@ -557,39 +549,32 @@ func getOApiVMAttributesSchema() map[string]*schema.Schema {
},
"nics": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"delete_on_vm_deletion": {
Type: schema.TypeBool,
Computed: true,
Optional: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},
"device_number": {
Type: schema.TypeInt,
Computed: true,
Optional: true,
},
"nic_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"private_ips": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"is_primary": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
"link_public_ip": {
Expand Down Expand Up @@ -618,20 +603,17 @@ func getOApiVMAttributesSchema() map[string]*schema.Schema {
},
"private_ip": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
},
},
"secondary_private_ip_count": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"security_group_ids": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
Expand All @@ -648,7 +630,6 @@ func getOApiVMAttributesSchema() map[string]*schema.Schema {
"subnet_id": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},
"link_nic": {
Type: schema.TypeList,
Expand Down Expand Up @@ -710,7 +691,6 @@ func getOApiVMAttributesSchema() map[string]*schema.Schema {
},
"security_groups_names": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
Expand Down Expand Up @@ -747,12 +727,10 @@ func getOApiVMAttributesSchema() map[string]*schema.Schema {
},
"placement_subregion_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"placement_tenancy": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"private_dns_name": {
Expand Down Expand Up @@ -814,8 +792,6 @@ func getOApiVMAttributesSchema() map[string]*schema.Schema {
},
"subnet_id": {
Type: schema.TypeString,
ForceNew: true,
Optional: true,
Computed: true,
},
"tags": {
Expand All @@ -836,22 +812,19 @@ func getOApiVMAttributesSchema() map[string]*schema.Schema {
},
"user_data": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"vm_id": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Computed: true,
},
"vm_initiated_shutdown_behavior": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"vm_type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"request_id": {
Expand All @@ -860,7 +833,7 @@ func getOApiVMAttributesSchema() map[string]*schema.Schema {
},
"private_ips": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
}
Expand Down
8 changes: 3 additions & 5 deletions outscale/resource_outscale_ca.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/openlyinc/pointy"
oscgo "github.com/outscale/osc-sdk-go/v2"
"github.com/spf13/cast"
"github.com/terraform-providers/terraform-provider-outscale/utils"
Expand Down Expand Up @@ -58,8 +57,8 @@ func resourceOutscaleOAPICaCreate(d *schema.ResourceData, meta interface{}) erro
req := oscgo.CreateCaRequest{
CaPem: d.Get("ca_pem").(string),
}
if v, ok := d.GetOk("description"); ok {
req.Description = pointy.String(v.(string))
if _, ok := d.GetOk("description"); ok {
req.SetDescription(d.Get("description").(string))
}

var resp oscgo.CreateCaResponse
Expand Down Expand Up @@ -130,8 +129,7 @@ func resourceOutscaleOAPICaUpdate(d *schema.ResourceData, meta interface{}) erro
}

if d.HasChange("description") {
_, des := d.GetChange("description")
req.Description = pointy.String(des.(string))
req.SetDescription(d.Get("description").(string))
}

var err error
Expand Down
8 changes: 3 additions & 5 deletions outscale/resource_outscale_dhcp_option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"testing"
"time"

"github.com/openlyinc/pointy"
"github.com/terraform-providers/terraform-provider-outscale/utils"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
Expand Down Expand Up @@ -122,10 +121,9 @@ func TestAccNet_withDhcpOptional(t *testing.T) {
domainName := fmt.Sprintf("%s.compute%s.internal", utils.GetRegion(), acctest.RandString(3))
domainServers := []string{"192.168.12.12", "192.168.12.132"}

tags := &oscgo.Tag{
Key: pointy.String(acctest.RandomWithPrefix("name")),
Value: pointy.String(acctest.RandomWithPrefix("test-MZI")),
}
tags := &oscgo.Tag{}
tags.SetKey(acctest.RandomWithPrefix("name"))
tags.SetValue(acctest.RandomWithPrefix("test-MZI"))

domainNameUpdated := fmt.Sprintf("%s.compute%s.internal", utils.GetRegion(), acctest.RandString(3))

Expand Down
7 changes: 2 additions & 5 deletions outscale/resource_outscale_image_export_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/terraform-providers/terraform-provider-outscale/utils"

"github.com/openlyinc/pointy"
oscgo "github.com/outscale/osc-sdk-go/v2"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
Expand Down Expand Up @@ -146,10 +145,8 @@ func resourceOAPIImageExportTaskCreate(d *schema.ResourceData, meta interface{})
a := v.([]interface{})
if len(a) > 0 {
w := a[0].(map[string]interface{})
et.OsuApiKey = &oscgo.OsuApiKey{
ApiKeyId: pointy.String(w["api_key_id"].(string)),
SecretKey: pointy.String(w["secret_key"].(string)),
}
et.OsuApiKey.SetApiKeyId(w["api_key_id"].(string))
et.OsuApiKey.SetSecretKey(w["secret_key"].(string))
}
}
if v, ok := e["osu_manifest_url"]; ok {
Expand Down
27 changes: 6 additions & 21 deletions outscale/resource_outscale_nic.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/openlyinc/pointy"
"github.com/terraform-providers/terraform-provider-outscale/utils"
)

Expand Down Expand Up @@ -291,11 +290,7 @@ func resourceOutscaleOAPINicCreate(d *schema.ResourceData, meta interface{}) err
if err := d.Set("private_ip", ""); err != nil {
return err
}

log.Printf("[INFO] ENI ID: %s", d.Id())

return resourceOutscaleOAPINicRead(d, meta)

}

// Read OAPINic
Expand Down Expand Up @@ -327,7 +322,6 @@ func resourceOutscaleOAPINicRead(d *schema.ResourceData, meta interface{}) error

if err != nil {
if strings.Contains(err.Error(), "Unable to find Nic") {
// The ENI is gone now, so just remove it from the state
d.SetId("")
return nil
}
Expand Down Expand Up @@ -368,11 +362,6 @@ func resourceOutscaleOAPINicRead(d *schema.ResourceData, meta interface{}) error
bb["vm_account_id"] = att.GetVmAccountId()
bb["state"] = att.GetState()

//aa[0] = bb
// if err := d.Set("link_nic", aa); err != nil {
// return err
// }

if err := d.Set("link_nic", bb); err != nil {
return err
}
Expand Down Expand Up @@ -607,6 +596,9 @@ func resourceOutscaleOAPINicUpdate(d *schema.ResourceData, meta interface{}) err
pips := d.Get("pips").(*schema.Set).List()
prips := pips[:0]
pip := d.Get("private_ip")
input := oscgo.LinkPrivateIpsRequest{
NicId: d.Id(),
}

for _, ip := range pips {
if ip != pip {
Expand All @@ -619,12 +611,7 @@ func resourceOutscaleOAPINicUpdate(d *schema.ResourceData, meta interface{}) err

// Surplus of IPs, add the diff
if diff > 0 {
dif := int32(diff)
input := oscgo.LinkPrivateIpsRequest{
NicId: d.Id(),
SecondaryPrivateIpCount: pointy.Int32(dif),
}
// _, err := conn.VM.AssignPrivateIpAddresses(input)
input.SetSecondaryPrivateIpCount(int32(diff))

err := resource.Retry(5*time.Minute, func() *resource.RetryError {
var err error
Expand Down Expand Up @@ -677,15 +664,13 @@ func resourceOutscaleOAPINicUpdate(d *schema.ResourceData, meta interface{}) err
if err != nil {
return fmt.Errorf("Failure updating ENI: %s", err)
}

}

if d.HasChange("description") {
request := oscgo.UpdateNicRequest{
NicId: d.Id(),
Description: pointy.String(d.Get("description").(string)),
NicId: d.Id(),
}

request.SetDescription(d.Get("description").(string))
var err error
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
_, httpResp, err := conn.NicApi.UpdateNic(context.Background()).UpdateNicRequest(request).Execute()
Expand Down
Loading

0 comments on commit 472cadf

Please sign in to comment.