Skip to content

Commit

Permalink
Move some function to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
FredericBerot-Armand committed Nov 24, 2022
1 parent 055f3ec commit 8c80f07
Show file tree
Hide file tree
Showing 31 changed files with 133 additions and 156 deletions.
4 changes: 2 additions & 2 deletions outscale/data_source_outscale_access_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ func dataSourceAccessKeysRead(d *schema.ResourceData, meta interface{}) error {
filterReq = buildDataSourceAccessKeyFilters(filters.(*schema.Set))
}
if accessKeyOk {
filterReq.SetAccessKeyIds(expandStringValueList(accessKeyID.([]interface{})))
filterReq.SetAccessKeyIds(utils.InterfaceSliceToStringSlice(accessKeyID.([]interface{})))
}
if stateOk {
filterReq.SetStates(expandStringValueList(state.([]interface{})))
filterReq.SetStates(utils.InterfaceSliceToStringSlice(state.([]interface{})))
}

var resp oscgo.ReadAccessKeysResponse
Expand Down
2 changes: 1 addition & 1 deletion outscale/data_source_outscale_accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func flattenAccounts(accounts []oscgo.Account) []map[string]interface{} {
for i, account := range accounts {
accountsMap[i] = map[string]interface{}{
"account_id": account.GetAccountId(),
"additional_emails": flattenStringList(account.AdditionalEmails),
"additional_emails": utils.StringListToInterfaceSlice(account.AdditionalEmails),
"city": account.GetCity(),
"company_name": account.GetCompanyName(),
"country": account.GetCountry(),
Expand Down
2 changes: 1 addition & 1 deletion outscale/data_source_outscale_client_gateways.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func dataSourceClientGatewaysRead(d *schema.ResourceData, meta interface{}) erro

if clientGatewayOk {
params.Filters = &oscgo.FiltersClientGateway{
ClientGatewayIds: expandStringValueListPointer(clientGatewayIDs.([]interface{})),
ClientGatewayIds: utils.InterfaceSliceToStringList(clientGatewayIDs.([]interface{})),
}
}

Expand Down
2 changes: 1 addition & 1 deletion outscale/data_source_outscale_dhcp_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func dataSourceDHCPOptionsRead(d *schema.ResourceData, meta interface{}) error {
params := oscgo.ReadDhcpOptionsRequest{}
if dhcpIDOk {
params.Filters = &oscgo.FiltersDhcpOptions{
DhcpOptionsSetIds: expandStringValueListPointer(dhcpIDs.([]interface{})),
DhcpOptionsSetIds: utils.InterfaceSliceToStringList(dhcpIDs.([]interface{})),
}
}
if filtersOk {
Expand Down
2 changes: 1 addition & 1 deletion outscale/data_source_outscale_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func dataSourceImageRead(d *schema.ResourceData, meta interface{}) error {
filtersReq.SetAccountIds([]string{ai.(string)})
}
if executableUsersOk {
filtersReq.SetPermissionsToLaunchAccountIds(expandStringValueList(executableUsers.([]interface{})))
filtersReq.SetPermissionsToLaunchAccountIds(utils.InterfaceSliceToStringSlice(executableUsers.([]interface{})))
}

req := oscgo.ReadImagesRequest{Filters: filtersReq}
Expand Down
18 changes: 1 addition & 17 deletions outscale/data_source_outscale_images.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func dataSourceImagesRead(d *schema.ResourceData, meta interface{}) error {
filtersReq.SetAccountIds([]string{aids.(string)})
}
if executableUsersOk {
filtersReq.SetPermissionsToLaunchAccountIds(expandStringValueList(executableUsers.([]interface{})))
filtersReq.SetPermissionsToLaunchAccountIds(utils.InterfaceSliceToStringSlice(executableUsers.([]interface{})))
}

req := oscgo.ReadImagesRequest{Filters: filtersReq}
Expand Down Expand Up @@ -344,19 +344,3 @@ func buildDataSourceImagesFilters(set *schema.Set) *oscgo.FiltersImage {
}
return filters
}

func expandStringValueList(configured []interface{}) []string {
vs := make([]string, 0, len(configured))
for _, v := range configured {
val, ok := v.(string)
if ok && val != "" {
vs = append(vs, v.(string))
}
}
return vs
}

func expandStringValueListPointer(configured []interface{}) *[]string {
res := expandStringValueList(configured)
return &res
}
10 changes: 5 additions & 5 deletions outscale/data_source_outscale_load_balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func readLbs_(conn *oscgo.APIClient, d *schema.ResourceData, t schema.ValueType)
}
} else { /* assuming typelist */
filter = &oscgo.FiltersLoadBalancer{
LoadBalancerNames: expandStringList(ename.([]interface{})),
LoadBalancerNames: utils.InterfaceSliceToStringSlicePtr(ename.([]interface{})),
}

}
Expand Down Expand Up @@ -286,12 +286,12 @@ func dataSourceLoadBalancerRead(d *schema.ResourceData, meta interface{}) error
return err
}

d.Set("subregion_names", flattenStringList(lb.SubregionNames))
d.Set("subregion_names", utils.StringListToInterfaceSlice(lb.SubregionNames))
d.Set("dns_name", lb.DnsName)
d.Set("health_check", flattenHealthCheck(lb.HealthCheck))
d.Set("access_log", flattenAccessLog(lb.AccessLog))

d.Set("backend_vm_ids", flattenStringList(lb.BackendVmIds))
d.Set("backend_vm_ids", utils.StringListToInterfaceSlice(lb.BackendVmIds))
if err := d.Set("listeners", flattenListeners(lb.Listeners)); err != nil {
return err
}
Expand Down Expand Up @@ -337,7 +337,7 @@ func dataSourceLoadBalancerRead(d *schema.ResourceData, meta interface{}) error
}
d.Set("load_balancer_type", lb.LoadBalancerType)
if lb.SecurityGroups != nil {
d.Set("security_groups", flattenStringList(lb.SecurityGroups))
d.Set("security_groups", utils.StringListToInterfaceSlice(lb.SecurityGroups))
} else {
d.Set("security_groups", make([]map[string]interface{}, 0))
}
Expand All @@ -351,7 +351,7 @@ func dataSourceLoadBalancerRead(d *schema.ResourceData, meta interface{}) error
d.Set("secured_cookies", lb.SecuredCookies)
d.Set("net_id", lb.NetId)
d.Set("source_security_group", ssg)
d.Set("subnets", flattenStringList(lb.Subnets))
d.Set("subnets", utils.StringListToInterfaceSlice(lb.Subnets))
d.SetId(*lb.LoadBalancerName)

return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func dataSourceLoadBalancerLDRuleRead(d *schema.ResourceData, meta interface{})
}

if lr.VmIds != nil {
d.Set("vm_ids", flattenStringList(lr.VmIds))
d.Set("vm_ids", utils.StringListToInterfaceSlice(lr.VmIds))
} else {
fmt.Errorf("Malformated listener rule")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func dataSourceLoadBalancerLDRulesRead(d *schema.ResourceData, meta interface{})
}

if lr.VmIds != nil {
l["vm_ids"] = flattenStringList(lr.VmIds)
l["vm_ids"] = utils.StringListToInterfaceSlice(lr.VmIds)
} else {
fmt.Errorf("Malformated listener rule")
}
Expand Down
2 changes: 1 addition & 1 deletion outscale/data_source_outscale_load_balancer_tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func dataSourceLBUTagsRead(d *schema.ResourceData, meta interface{}) error {
names := ename.([]interface{})

req := oscgo.ReadLoadBalancerTagsRequest{
LoadBalancerNames: *expandStringList(names),
LoadBalancerNames: utils.InterfaceSliceToStringSlice(names),
}

var resp oscgo.ReadLoadBalancerTagsResponse
Expand Down
3 changes: 2 additions & 1 deletion outscale/data_source_outscale_load_balancer_vms.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package outscale

import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-outscale/utils"
)

func dataSourceLoadBalancerVms() *schema.Resource {
Expand Down Expand Up @@ -37,6 +38,6 @@ func dataSourceLoadBalancerVmsRead(d *schema.ResourceData,
return err
}

d.Set("backend_vm_ids", flattenStringList(lb.BackendVmIds))
d.Set("backend_vm_ids", utils.StringListToInterfaceSlice(lb.BackendVmIds))
return nil
}
7 changes: 4 additions & 3 deletions outscale/data_source_outscale_load_balancers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package outscale
import (
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-outscale/utils"
)

func attrLBSchema() map[string]*schema.Schema {
Expand Down Expand Up @@ -218,7 +219,7 @@ func dataSourceLoadBalancersRead(d *schema.ResourceData, meta interface{}) error
l["dns_name"] = *v.DnsName
l["access_log"] = flattenAccessLog(v.AccessLog)
l["health_check"] = flattenHealthCheck(v.HealthCheck)
l["backend_vm_ids"] = flattenStringList(v.BackendVmIds)
l["backend_vm_ids"] = utils.StringListToInterfaceSlice(v.BackendVmIds)
if v.Listeners != nil {
l["listeners"] = flattenListeners(v.Listeners)
} else {
Expand Down Expand Up @@ -265,15 +266,15 @@ func dataSourceLoadBalancersRead(d *schema.ResourceData, meta interface{}) error
}

l["load_balancer_type"] = v.LoadBalancerType
l["security_groups"] = flattenStringList(v.SecurityGroups)
l["security_groups"] = utils.StringListToInterfaceSlice(v.SecurityGroups)
ssg := make(map[string]string)

if v.SourceSecurityGroup != nil {
ssg["security_group_account_id"] = *v.SourceSecurityGroup.SecurityGroupAccountId
ssg["security_group_name"] = *v.SourceSecurityGroup.SecurityGroupName
}
l["source_security_group"] = ssg
l["subnet_id"] = flattenStringList(v.Subnets)
l["subnet_id"] = utils.StringListToInterfaceSlice(v.Subnets)
l["public_ip"] = v.PublicIp
l["secured_cookies"] = v.SecuredCookies
l["net_id"] = v.NetId
Expand Down
2 changes: 1 addition & 1 deletion outscale/data_source_outscale_net_access_point.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func dataSourceNetAccessPointRead(d *schema.ResourceData, meta interface{}) erro
nap := resp.GetNetAccessPoints()[0]

d.Set("net_access_point_id", nap.NetAccessPointId)
d.Set("route_table_ids", flattenStringList(nap.RouteTableIds))
d.Set("route_table_ids", utils.StringListToInterfaceSlice(nap.RouteTableIds))
d.Set("net_id", nap.NetId)
d.Set("service_name", nap.ServiceName)
d.Set("state", nap.State)
Expand Down
2 changes: 1 addition & 1 deletion outscale/data_source_outscale_net_access_points.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func dataSourceNetAccessPointsRead(d *schema.ResourceData, meta interface{}) err
n := make(map[string]interface{})

n["net_access_point_id"] = v.NetAccessPointId
n["route_table_ids"] = flattenStringList(v.RouteTableIds)
n["route_table_ids"] = utils.StringListToInterfaceSlice(v.RouteTableIds)
n["net_id"] = v.NetId
n["service_name"] = v.ServiceName
n["state"] = v.State
Expand Down
14 changes: 1 addition & 13 deletions outscale/data_source_outscale_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func dataSourceSnapshotRead(d *schema.ResourceData, meta interface{}) error {

filter := oscgo.FiltersSnapshot{}
if restorableUsersOk {
filter.SetPermissionsToCreateVolumeAccountIds(ExpandStringList(restorableUsers.([]interface{})))
filter.SetPermissionsToCreateVolumeAccountIds(utils.InterfaceSliceToStringSlice(restorableUsers.([]interface{})))
params.SetFilters(filter)
}
if filtersOk {
Expand Down Expand Up @@ -245,15 +245,3 @@ func buildSnapshotDataSourceFilters(set *schema.Set, filter *oscgo.FiltersSnapsh
}
return filter
}

// TODO MOVE To utils
func ExpandStringList(configured []interface{}) []string {
vs := make([]string, 0, len(configured))
for _, v := range configured {
val, ok := v.(string)
if ok && val != "" {
vs = append(vs, v.(string))
}
}
return vs
}
6 changes: 3 additions & 3 deletions outscale/data_source_outscale_snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,18 @@ func dataSourceSnapshotsRead(d *schema.ResourceData, meta interface{}) error {
}
filter := oscgo.FiltersSnapshot{}
if restorableUsersOk {
filter.SetPermissionsToCreateVolumeAccountIds(ExpandStringList(restorableUsers.([]interface{})))
filter.SetPermissionsToCreateVolumeAccountIds(utils.InterfaceSliceToStringSlice(restorableUsers.([]interface{})))
params.SetFilters(filter)
}
if filtersOk {
buildSnapshotDataSourceFilters(filters.(*schema.Set), params.Filters)
}
if ownersOk {
filter.SetAccountIds(ExpandStringList(owners.([]interface{})))
filter.SetAccountIds(utils.InterfaceSliceToStringSlice(owners.([]interface{})))
params.SetFilters(filter)
}
if snapshotIdsOk {
filter.SetSnapshotIds(ExpandStringList(snapshotIds.([]interface{})))
filter.SetSnapshotIds(utils.InterfaceSliceToStringSlice(snapshotIds.([]interface{})))
params.SetFilters(filter)
}

Expand Down
2 changes: 1 addition & 1 deletion outscale/data_source_outscale_vm_states.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func dataSourceVMStatesRead(d *schema.ResourceData, meta interface{}) error {
}
if instanceIdsOk {
filter := oscgo.FiltersVmsState{}
filter.SetVmIds(ExpandStringList(instanceIds.([]interface{})))
filter.SetVmIds(utils.InterfaceSliceToStringSlice(instanceIds.([]interface{})))
params.SetFilters(filter)
}

Expand Down
2 changes: 1 addition & 1 deletion outscale/data_source_outscale_volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func datasourceVolumesRead(d *schema.ResourceData, meta interface{}) error {
}

if volumeIdsOk {
volIDs := expandStringValueList(volumeIds.([]interface{}))
volIDs := utils.InterfaceSliceToStringSlice(volumeIds.([]interface{}))
filter := oscgo.FiltersVolume{}
filter.SetVolumeIds(volIDs)
params.SetFilters(filter)
Expand Down
2 changes: 1 addition & 1 deletion outscale/data_source_outscale_vpn_connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func dataSourceVPNConnectionsRead(d *schema.ResourceData, meta interface{}) erro

if vpnConnectionOk {
params.Filters = &oscgo.FiltersVpnConnection{
VpnConnectionIds: expandStringValueListPointer(vpnConnectionIDs.([]interface{})),
VpnConnectionIds: utils.InterfaceSliceToStringSlicePtr(vpnConnectionIDs.([]interface{})),
}
}

Expand Down
12 changes: 6 additions & 6 deletions outscale/resource_outscale_api_access_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,18 @@ func resourceApiAccessRuleCreate(d *schema.ResourceData, meta interface{}) error

if val, ok := d.GetOk("ca_ids"); ok {
checkParam = true
req.CaIds = expandSetStringList(val.(*schema.Set))
req.CaIds = utils.SetToStringSlicePtr(val.(*schema.Set))
}
if val, ok := d.GetOk("ip_ranges"); ok {
checkParam = true
req.IpRanges = expandSetStringList(val.(*schema.Set))
req.IpRanges = utils.SetToStringSlicePtr(val.(*schema.Set))
}
if !checkParam {
return fmt.Errorf("[DEBUG] Error 'ca_ids' or 'ip_ranges' field is require for API Access Rules creation")
}

if val, ok := d.GetOk("cns"); ok {
req.Cns = expandSetStringList(val.(*schema.Set))
req.Cns = utils.SetToStringSlicePtr(val.(*schema.Set))
}
if v, ok := d.GetOk("description"); ok {
req.SetDescription(v.(string))
Expand Down Expand Up @@ -169,19 +169,19 @@ func resourceApiAccessRuleUpdate(d *schema.ResourceData, meta interface{}) error

if val, ok := d.GetOk("ca_ids"); ok {
checkParam = true
req.CaIds = expandSetStringList(val.(*schema.Set))
req.CaIds = utils.SetToStringSlicePtr(val.(*schema.Set))
}
if val, ok := d.GetOk("ip_ranges"); ok {
checkParam = true
req.IpRanges = expandSetStringList(val.(*schema.Set))
req.IpRanges = utils.SetToStringSlicePtr(val.(*schema.Set))
}

if !checkParam {
return fmt.Errorf("[DEBUG] Error 'ca_ids' or 'ip_ranges' field is require to update API Access Rules")
}

if val, ok := d.GetOk("cns"); ok {
req.Cns = expandSetStringList(val.(*schema.Set))
req.Cns = utils.SetToStringSlicePtr(val.(*schema.Set))
}
if v, ok := d.GetOk("description"); ok {
req.SetDescription(v.(string))
Expand Down
6 changes: 3 additions & 3 deletions outscale/resource_outscale_dhcp_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ func resourceDHCPOptionCreate(d *schema.ResourceData, meta interface{}) error {
createOpts.SetDomainName(domainName.(string))
}
if okDomainNameServers {
createOpts.SetDomainNameServers(expandStringValueList(domainNameServers.([]interface{})))
createOpts.SetDomainNameServers(utils.InterfaceSliceToStringSlice(domainNameServers.([]interface{})))
}
if okLogServers {
createOpts.SetLogServers(expandStringValueList(logServers.([]interface{})))
createOpts.SetLogServers(utils.InterfaceSliceToStringSlice(logServers.([]interface{})))
}
if okNTPServers {
createOpts.SetNtpServers(expandStringValueList(ntpServers.([]interface{})))
createOpts.SetNtpServers(utils.InterfaceSliceToStringSlice(ntpServers.([]interface{})))
}

dhcp, _, err := createDhcpOption(conn, createOpts)
Expand Down
Loading

0 comments on commit 8c80f07

Please sign in to comment.