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

exoscale_instance: use egoscale v3 #408

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 2 additions & 1 deletion docs/data-sources/compute_instance.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ directory for complete configuration examples.
- `public_ip_address` (String) The instance (main network interface) IPv4 address.
- `reverse_dns` (String) Domain name for reverse DNS record.
- `security_group_ids` (Set of String) The list of attached [exoscale_security_group](../resources/security_group.md) (IDs).
- `ssh_key` (String) The [exoscale_ssh_key](../resources/ssh_key.md) (name) authorized on the instance.
- `ssh_key` (String, Deprecated) The [exoscale_ssh_key](../resources/ssh_key.md) (name) authorized on the instance.
- `ssh_keys` (Set of String) The list of [exoscale_ssh_key](../resources/ssh_key.md) (name) authorized on the instance.
- `state` (String) The instance state.
- `template_id` (String) The instance [exoscale_template](./template.md) ID.
- `type` (String) The instance type.
Expand Down
1 change: 1 addition & 0 deletions docs/data-sources/compute_instance_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ Read-Only:
- `reverse_dns` (String)
- `security_group_ids` (Set of String)
- `ssh_key` (String)
- `ssh_keys` (Set of String)
- `state` (String)
- `template_id` (String)
- `type` (String)
Expand Down
3 changes: 2 additions & 1 deletion docs/resources/compute_instance.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ directory for complete configuration examples.
- `private` (Boolean) Whether the instance is private (no public IP addresses; default: false)
- `reverse_dns` (String) Domain name for reverse DNS record.
- `security_group_ids` (Set of String) A list of [exoscale_security_group](./security_group.md) (IDs) to attach to the instance.
- `ssh_key` (String) The [exoscale_ssh_key](./ssh_key.md) (name) to authorize in the instance (may only be set at creation time).
- `ssh_key` (String, Deprecated) The [exoscale_ssh_key](./ssh_key.md) (name) to authorize in the instance (may only be set at creation time).
- `ssh_keys` (Set of String) The list of [exoscale_ssh_key](./ssh_key.md) (name) to authorize in the instance (may only be set at creation time).
- `state` (String) The instance state (`running` or `stopped`; default: `running`).
- `timeouts` (Block, Optional) (see [below for nested schema](#nestedblock--timeouts))
- `user_data` (String) [cloud-init](https://cloudinit.readthedocs.io/) configuration.
Expand Down
4 changes: 1 addition & 3 deletions exoscale/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,16 +238,14 @@ func ProviderConfigure(_ context.Context, d *schema.ResourceData) (interface{},

opts := []exov3.ClientOpt{}
if ep := os.Getenv("EXOSCALE_API_ENDPOINT"); ep != "" {
opts = append(opts, exov3.ClientOptWithEndpoint(exov3.Endpoint(ep)))
opts = append(opts, exov3.ClientOptWithEndpoint(exov3.Endpoint(ep)), exov3.ClientOptWithUserAgent(UserAgent))
}

clv3, err := exov3.NewClient(creds, opts...)
if err != nil {
return nil, diag.FromErr(err)
}

exov3.UserAgent = UserAgent

return map[string]interface{}{
"config": baseConfig,
"client": clv2,
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require (
github.com/aws/aws-sdk-go-v2/credentials v1.17.42
github.com/aws/aws-sdk-go-v2/service/s3 v1.66.2
github.com/exoscale/egoscale v0.102.4
github.com/exoscale/egoscale/v3 v3.1.1
github.com/exoscale/egoscale/v3 v3.1.7
github.com/google/go-cmp v0.6.0
github.com/hashicorp/go-cleanhttp v0.5.2
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
Expand All @@ -21,7 +21,7 @@ require (
github.com/hashicorp/terraform-plugin-sdk/v2 v2.34.0
github.com/hashicorp/terraform-plugin-testing v1.10.0
github.com/ssgreg/repeat v1.5.1
github.com/stretchr/testify v1.8.4
github.com/stretchr/testify v1.9.0
github.com/xeipuuv/gojsonschema v1.2.0
gopkg.in/yaml.v3 v3.0.1
)
Expand Down Expand Up @@ -102,7 +102,7 @@ require (
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.18.2 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
Expand Down
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc
github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ=
github.com/exoscale/egoscale v0.102.4 h1:GBKsZMIOzwBfSu+4ZmWka3Ejf2JLiaBDHp4CQUgvp2E=
github.com/exoscale/egoscale v0.102.4/go.mod h1:ROSmPtle0wvf91iLZb09++N/9BH2Jo9XxIpAEumvocA=
github.com/exoscale/egoscale/v3 v3.1.1 h1:NwTlXE2sKe2kBWm+c3bsHV+aWDFiEJ9JQpS6X3j4wbc=
github.com/exoscale/egoscale/v3 v3.1.1/go.mod h1:lPsza7G+giSxdzvzaHSEcjEAYz/YTiu2bEEha9KVAc4=
github.com/exoscale/egoscale/v3 v3.1.7 h1:Q6p9tOVY0IiOW0fUpaPQWY7ggGEuSPZLAGxFgDd2sCE=
github.com/exoscale/egoscale/v3 v3.1.7/go.mod h1:GHKucK/J26v8PGWztGdhxWNMjrjG9PbelxKCJ4YI11Q=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
Expand Down Expand Up @@ -316,8 +316,9 @@ github.com/ssgreg/repeat v1.5.1 h1:8OjfXKWnFU9cL1cI+2UCdPpOpGOEax1oZ1FQdylri+8=
github.com/ssgreg/repeat v1.5.1/go.mod h1:V1zMJmma0AQitsevwH3wM/uFcIw6VxW0dHBJBhajl/o=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
Expand All @@ -327,8 +328,9 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
Expand Down
4 changes: 1 addition & 3 deletions pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,16 +172,14 @@ func (p *ExoscaleProvider) Configure(ctx context.Context, req provider.Configure

opts := []exov3.ClientOpt{}
if ep := os.Getenv("EXOSCALE_API_ENDPOINT"); ep != "" {
opts = append(opts, exov3.ClientOptWithEndpoint(exov3.Endpoint(ep)))
opts = append(opts, exov3.ClientOptWithEndpoint(exov3.Endpoint(ep)), exov3.ClientOptWithUserAgent(exoscale.UserAgent))
}

clv3, err := exov3.NewClient(creds, opts...)
if err != nil {
resp.Diagnostics.AddError(err.Error(), "unable to initialize Exoscale API V3 client")
}

exov3.UserAgent = exoscale.UserAgent

resp.DataSourceData = &providerConfig.ExoscaleProviderConfig{
Config: baseConfig,
ClientV2: clv2,
Expand Down
1 change: 1 addition & 0 deletions pkg/resources/instance/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const (
AttrPrivate = "private"
AttrReverseDNS = "reverse_dns"
AttrSSHKey = "ssh_key"
AttrSSHKeys = "ssh_keys"
AttrSecurityGroupIDs = "security_group_ids"
AttrState = "state"
AttrTemplateID = "template_id"
Expand Down
153 changes: 111 additions & 42 deletions pkg/resources/instance/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

exo "github.com/exoscale/egoscale/v2"
exoapi "github.com/exoscale/egoscale/v2/api"
v3 "github.com/exoscale/egoscale/v3"

"github.com/exoscale/terraform-provider-exoscale/pkg/config"
"github.com/exoscale/terraform-provider-exoscale/pkg/utils"
Expand Down Expand Up @@ -106,6 +105,14 @@ func DataSourceSchema() map[string]*schema.Schema {
Description: "The [exoscale_ssh_key](../resources/ssh_key.md) (name) authorized on the instance.",
Type: schema.TypeString,
Computed: true,
Deprecated: "Use ssh_keys instead",
},
AttrSSHKeys: {
Description: "The list of [exoscale_ssh_key](../resources/ssh_key.md) (name) authorized on the instance.",
Type: schema.TypeSet,
Computed: true,
Set: schema.HashString,
Elem: &schema.Schema{Type: schema.TypeString},
},
AttrSecurityGroupIDs: {
Description: "The list of attached [exoscale_security_group](../resources/security_group.md) (IDs).",
Expand Down Expand Up @@ -167,10 +174,17 @@ func dsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.
zone := d.Get(AttrZone).(string)

ctx, cancel := context.WithTimeout(ctx, d.Timeout(schema.TimeoutRead))
ctx = exoapi.WithEndpoint(ctx, exoapi.NewReqEndpoint(config.GetEnvironment(meta), zone))
defer cancel()

client, err := config.GetClient(meta)
defaultClientV3, err := config.GetClientV3(meta)
if err != nil {
return diag.FromErr(err)
}
client, err := utils.SwitchClientZone(
ctx,
defaultClientV3,
v3.ZoneName(zone),
)
if err != nil {
return diag.FromErr(err)
}
Expand All @@ -185,46 +199,55 @@ func dsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.
)
}

instance, err := client.FindInstance(
ctx,
zone, func() string {
instanceList, err := client.ListInstances(ctx)
if err != nil {
return diag.FromErr(err)
}

instanceListResp, err := instanceList.FindListInstancesResponseInstances(
func() string {
if byID {
return id.(string)
} else {
return name.(string)
}
return name.(string)
}(),
)
if err != nil {
return diag.FromErr(err)
return diag.Errorf("unable to retrieve instance: %s", err)
}

d.SetId(*instance.ID)
instance, err := client.GetInstance(ctx, instanceListResp.ID)
if err != nil {
return diag.Errorf("unable to retrieve instance: %s", err)
}

d.SetId(string(instance.ID))

data, err := dsBuildData(instance)
data, err := dsBuildData(instance, zone)
if err != nil {
return diag.FromErr(err)
}

instanceType, err := client.GetInstanceType(
ctx,
zone,
*instance.InstanceTypeID,
instance.InstanceType.ID,
)
if err != nil {
return diag.Errorf("unable to retrieve instance type: %s", err)
}

data[AttrType] = fmt.Sprintf(
"%s.%s",
strings.ToLower(*instanceType.Family),
strings.ToLower(*instanceType.Size),
strings.ToLower(string(instanceType.Family)),
strings.ToLower(string(instanceType.Size)),
)

rdns, err := client.GetInstanceReverseDNS(ctx, zone, *instance.ID)
if err != nil && !errors.Is(err, exoapi.ErrNotFound) {
rdns, err := client.GetReverseDNSInstance(ctx, instance.ID)
if err != nil && !errors.Is(err, v3.ErrNotFound) {
return diag.Errorf("unable to retrieve instance reverse-dns: %s", err)
}
data[AttrReverseDNS] = strings.TrimSuffix(rdns, ".")
data[AttrReverseDNS] = strings.TrimSuffix(string(rdns.DomainName), ".")

for key, value := range data {
err := d.Set(key, value)
Expand All @@ -241,55 +264,101 @@ func dsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.
}

// dsBuildData builds terraform data object from egoscale API struct.
func dsBuildData(instance *exo.Instance) (map[string]interface{}, error) {
func dsBuildData(instance *v3.Instance, zone string) (map[string]interface{}, error) {
data := map[string]interface{}{}

data[AttrDeployTargetID] = instance.DeployTargetID
data[AttrDiskSize] = instance.DiskSize
data[AttrID] = instance.ID
data[AttrName] = instance.Name
data[AttrSSHKey] = instance.SSHKey
data[AttrState] = instance.State
data[AttrTemplateID] = instance.TemplateID
data[AttrZone] = instance.Zone
data[AttrZone] = zone

data[AttrIPv6] = func() bool { return instance.PublicIPAssignment == v3.PublicIPAssignmentDual }()

data[AttrDeployTargetID] = func() (s string) {
if instance.DeployTarget != nil {
s = instance.DeployTarget.ID.String()
} else {
s = ""
}
return
}()

if instance.SSHKey != nil {
data[AttrSSHKey] = instance.SSHKey.Name
}

if instance.Template != nil {
data[AttrTemplateID] = instance.Template.ID.String()
}

data[AttrIPv6] = utils.DefaultBool(instance.IPv6Enabled, false)
if instance.SSHKeys != nil {
data[AttrSSHKeys] = func() []string {
list := make([]string, len(instance.SSHKeys))
for i, k := range instance.SSHKeys {
list[i] = k.Name
}
return list
}()
}

if instance.ElasticIPIDs != nil {
data[AttrElasticIPIDs] = *instance.ElasticIPIDs
if instance.ElasticIPS != nil {
data[AttrElasticIPIDs] = func() []string {
list := make([]string, len(instance.ElasticIPS))
for i, eip := range instance.ElasticIPS {
list[i] = eip.ID.String()
}
return list
}()
}
if instance.AntiAffinityGroupIDs != nil {
data[AttrAntiAffinityGroupIDs] = *instance.AntiAffinityGroupIDs
if instance.AntiAffinityGroups != nil {
data[AttrAntiAffinityGroupIDs] = func() []string {
list := make([]string, len(instance.AntiAffinityGroups))
for i, aag := range instance.AntiAffinityGroups {
list[i] = aag.ID.String()
}
return list
}()
}
if instance.Labels != nil {
data[AttrLabels] = *instance.Labels
data[AttrLabels] = instance.Labels
}
if instance.PrivateNetworkIDs != nil {
data[AttrPrivateNetworkIDs] = *instance.PrivateNetworkIDs
if instance.PrivateNetworks != nil {
data[AttrPrivateNetworkIDs] = func() []string {
list := make([]string, len(instance.PrivateNetworks))
for i, pn := range instance.PrivateNetworks {
list[i] = pn.ID.String()
}
return list
}()
}
if instance.SecurityGroupIDs != nil {
data[AttrSecurityGroupIDs] = *instance.SecurityGroupIDs
if instance.SecurityGroups != nil {
data[AttrSecurityGroupIDs] = func() []string {
list := make([]string, len(instance.SecurityGroups))
for i, sg := range instance.SecurityGroups {
list[i] = sg.ID.String()
}
return list
}()
}

if instance.Manager != nil {
data[AttrManagerID] = instance.Manager.ID
data[AttrManagerType] = instance.Manager.Type
}

if instance.CreatedAt != nil {
data[AttrCreatedAt] = instance.CreatedAt.String()
}
data[AttrCreatedAt] = instance.CreatedAT.String()

if instance.IPv6Address != nil {
data[AttrIPv6Address] = instance.IPv6Address.String()
if instance.Ipv6Address != "" {
data[AttrIPv6Address] = instance.Ipv6Address
}

if instance.PublicIPAddress != nil {
data[AttrPublicIPAddress] = instance.PublicIPAddress.String()
if instance.PublicIP.String() != "" {
data[AttrPublicIPAddress] = instance.PublicIP.String()
}

if instance.UserData != nil {
userData, err := utils.DecodeUserData(*instance.UserData)
if instance.UserData != "" {
userData, err := utils.DecodeUserData(instance.UserData)
if err != nil {
return nil, fmt.Errorf("unable to decode user data: %w", err)
}
Expand Down
Loading
Loading