Skip to content

Commit

Permalink
make helper function more intuitive
Browse files Browse the repository at this point in the history
  • Loading branch information
treywelsh committed Jul 10, 2023
1 parent ce63a4f commit b7a1775
Show file tree
Hide file tree
Showing 12 changed files with 29 additions and 30 deletions.
11 changes: 5 additions & 6 deletions opennebula/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func inArray(val string, array []string) (index int) {
var ok bool
for i := range array {
if ok = array[i] == val; ok {
return i
func contains(value string, values []string) bool {
for i := range values {
if values[i] == value {
return true
}
}
return -1
return false
}

func ArrayToString(list []interface{}, delim string) string {
Expand Down
2 changes: 1 addition & 1 deletion opennebula/resource_opennebula_datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func resourceOpennebulaDatastore() *schema.Resource {
for k := range datastoreTypes {
keys = append(keys, k)
}
if inArray(value, keys) < 0 {
if !contains(value, keys) {
errors = append(errors, fmt.Errorf("Type %q must be one of: %s", k, strings.Join(keys, ",")))
}

Expand Down
6 changes: 3 additions & 3 deletions opennebula/resource_opennebula_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func resourceOpennebulaGroup() *schema.Resource {
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)

if inArray(value, yesNo) < 0 {
if !contains(value, yesNo) {
errors = append(errors, fmt.Errorf("Type %q must be one of: %s", k, strings.Join(apiListOrder, ",")))
}

Expand All @@ -112,7 +112,7 @@ func resourceOpennebulaGroup() *schema.Resource {
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)

if inArray(value, yesNo) < 0 {
if !contains(value, yesNo) {
errors = append(errors, fmt.Errorf("Type %q must be one of: %s", k, strings.Join(apiListOrder, ",")))
}

Expand All @@ -126,7 +126,7 @@ func resourceOpennebulaGroup() *schema.Resource {
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)

if inArray(value, apiListOrder) < 0 {
if !contains(value, apiListOrder) {
errors = append(errors, fmt.Errorf("Type %q must be one of: %s", k, strings.Join(apiListOrder, ",")))
}

Expand Down
2 changes: 1 addition & 1 deletion opennebula/resource_opennebula_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func resourceOpennebulaHost() *schema.Resource {
Description: "Type of the new host: kvm, qemu, lxd, lxc, firecracker, custom",
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {

if inArray(v.(string), hostTypes) < 0 {
if !contains(v.(string), hostTypes) {
errors = append(errors, fmt.Errorf("host \"type\" must be one of: %s", strings.Join(hostTypes, ",")))
}

Expand Down
4 changes: 2 additions & 2 deletions opennebula/resource_opennebula_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func resourceOpennebulaImage() *schema.Resource {
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)

if inArray(value, imagetypes) < 0 {
if !contains(value, imagetypes) {
errors = append(errors, fmt.Errorf("Type %q must be one of: %s", k, strings.Join(imagetypes, ",")))
}

Expand Down Expand Up @@ -496,7 +496,7 @@ func resourceOpennebulaImageRead(ctx context.Context, d *schema.ResourceData, me
}
d.Set("path", image.Path)

if inArray(image.Type, imagetypes) >= 0 {
if contains(image.Type, imagetypes) {
d.Set("type", image.Type)
}

Expand Down
4 changes: 2 additions & 2 deletions opennebula/resource_opennebula_security_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func resourceOpennebulaSecurityGroup() *schema.Resource {
validprotos := []string{"ALL", "TCP", "UDP", "ICMP", "IPSEC"}
value := v.(string)

if inArray(value, validprotos) < 0 {
if !contains(value, validprotos) {
errors = append(errors, fmt.Errorf("Protocol %q must be one of: %s", k, strings.Join(validprotos, ",")))
}

Expand All @@ -115,7 +115,7 @@ func resourceOpennebulaSecurityGroup() *schema.Resource {
validtypes := []string{"INBOUND", "OUTBOUND"}
value := v.(string)

if inArray(value, validtypes) < 0 {
if !contains(value, validtypes) {
errors = append(errors, fmt.Errorf("Rule type %q must be one of: %s", k, strings.Join(validtypes, ",")))
}

Expand Down
10 changes: 5 additions & 5 deletions opennebula/resource_opennebula_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func FeaturesFields() map[string]*schema.Schema {
validtypes := []string{"YES", "NO"}
value := v.(string)

if inArray(value, validtypes) < 0 {
if !contains(value, validtypes) {
errors = append(errors, fmt.Errorf("PAE must be one of: %s", strings.Join(validtypes, ", ")))
}

Expand All @@ -127,7 +127,7 @@ func FeaturesFields() map[string]*schema.Schema {
validtypes := []string{"YES", "NO"}
value := v.(string)

if inArray(value, validtypes) < 0 {
if !contains(value, validtypes) {
errors = append(errors, fmt.Errorf("ACPI must be one of: %s", strings.Join(validtypes, ", ")))
}

Expand All @@ -142,7 +142,7 @@ func FeaturesFields() map[string]*schema.Schema {
validtypes := []string{"YES", "NO"}
value := v.(string)

if inArray(value, validtypes) < 0 {
if !contains(value, validtypes) {
errors = append(errors, fmt.Errorf("APIC must be one of: %s", strings.Join(validtypes, ", ")))
}

Expand All @@ -157,7 +157,7 @@ func FeaturesFields() map[string]*schema.Schema {
validtypes := []string{"YES", "NO"}
value := v.(string)

if inArray(value, validtypes) < 0 {
if !contains(value, validtypes) {
errors = append(errors, fmt.Errorf("LOCALTIME must be one of: %s", strings.Join(validtypes, ", ")))
}

Expand All @@ -177,7 +177,7 @@ func FeaturesFields() map[string]*schema.Schema {
validtypes := []string{"YES", "NO"}
value := v.(string)

if inArray(value, validtypes) < 0 {
if !contains(value, validtypes) {
errors = append(errors, fmt.Errorf("GUEST_AGENT must be one of: %s", strings.Join(validtypes, ", ")))
}

Expand Down
2 changes: 1 addition & 1 deletion opennebula/resource_opennebula_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func resourceOpennebulaUser() *schema.Resource {
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)

if inArray(value, authTypes) < 0 {
if !contains(value, authTypes) {
errors = append(errors, fmt.Errorf("Auth driver %q must be one of: %s", k, strings.Join(locktypes, ",")))
}

Expand Down
6 changes: 3 additions & 3 deletions opennebula/resource_opennebula_virtual_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func resourceOpennebulaVirtualNetwork() *schema.Resource {
validtypes := []string{"dummy", "bridge", "fw", "ebtables", "802.1Q", "vxlan", "ovswitch"}
value := v.(string)

if inArray(value, validtypes) < 0 {
if !contains(value, validtypes) {
errors = append(errors, fmt.Errorf("Type %q must be one of: %s", k, strings.Join(validtypes, ",")))
}

Expand Down Expand Up @@ -296,7 +296,7 @@ func ARFields() map[string]*schema.Schema {
validtypes := []string{"IP4", "IP6", "IP6_STATIC", "IP4_6", "IP4_6_STATIC", "ETHER"}
value := v.(string)

if inArray(value, validtypes) < 0 {
if !contains(value, validtypes) {
errors = append(errors, fmt.Errorf("Address Range type %q must be one of: %s", k, strings.Join(validtypes, ",")))
}

Expand Down Expand Up @@ -402,7 +402,7 @@ func changeVNetGroup(d *schema.ResourceData, meta interface{}) error {
}

func mandatoryVLAN(intype string) bool {
return inArray(intype, []string{"802.1Q", "vxlan"}) >= 0
return contains(intype, []string{"802.1Q", "vxlan"})
}

func resourceOpennebulaVirtualNetworkCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func resourceOpennebulaVirtualNetworkAddressRange() *schema.Resource {
validtypes := []string{"IP4", "IP6", "IP6_STATIC", "IP4_6", "IP4_6_STATIC", "ETHER"}
value := v.(string)

if inArray(value, validtypes) < 0 {
if !contains(value, validtypes) {
errors = append(errors, fmt.Errorf("Address Range type %q must be one of: %s", k, strings.Join(validtypes, ",")))
}

Expand Down
2 changes: 1 addition & 1 deletion opennebula/resource_opennebula_vm_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func resourceOpennebulaVMGroup() *schema.Resource {
validpolicies := []string{"NONE", "AFFINED", "ANTI_AFFINED"}
value := v.(string)

if inArray(value, validpolicies) < 0 {
if contains(value, validpolicies) {
errors = append(errors, fmt.Errorf("Policy value %q must be one of: %s", k, strings.Join(validpolicies, ",")))
}

Expand Down
8 changes: 4 additions & 4 deletions opennebula/shared_schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func commonVMSchemas() map[string]*schema.Schema {
Default: "swap", //"recreate" or "swap",
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
value := strings.ToUpper(v.(string))
if inArray(value, vmDiskOnChangeValues) == -1 {
if !contains(value, vmDiskOnChangeValues) {
errors = append(errors, fmt.Errorf("%q must be one of %s", k, strings.Join(vmDiskOnChangeValues, ", ")))
}
return
Expand Down Expand Up @@ -251,7 +251,7 @@ func diskFields(customFields ...map[string]*schema.Schema) map[string]*schema.Sc
validtypes := []string{"swap", "fs"}
value := v.(string)

if inArray(value, validtypes) < 0 {
if !contains(value, validtypes) {
errors = append(errors, fmt.Errorf("Type %q must be one of: %s", k, strings.Join(validtypes, ",")))
}

Expand All @@ -266,7 +266,7 @@ func diskFields(customFields ...map[string]*schema.Schema) map[string]*schema.Sc
validtypes := []string{"raw", "qcow2"}
value := v.(string)

if inArray(value, validtypes) < 0 {
if !contains(value, validtypes) {
errors = append(errors, fmt.Errorf("Format %q must be one of: %s", k, strings.Join(validtypes, ",")))
}

Expand Down Expand Up @@ -424,7 +424,7 @@ func lockSchema() *schema.Schema {
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)

if inArray(value, locktypes) < 0 {
if !contains(value, locktypes) {
errors = append(errors, fmt.Errorf("Type %q must be one of: %s", k, strings.Join(locktypes, ",")))
}

Expand Down

0 comments on commit b7a1775

Please sign in to comment.