Skip to content

Commit

Permalink
Fixed error reported in github
Browse files Browse the repository at this point in the history
Removed utils and put in the right folder, updated the civogo lib to the last version
Close #39
  • Loading branch information
alejandrojnm committed Jul 18, 2021
1 parent 34d310f commit 4e43800
Show file tree
Hide file tree
Showing 15 changed files with 109 additions and 53 deletions.
3 changes: 2 additions & 1 deletion civo/resource_dns_domain_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"

"github.com/civo/civogo"
"github.com/civo/terraform-provider-civo/internal/utils"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

Expand All @@ -17,7 +18,7 @@ func resourceDNSDomainName() *schema.Resource {
Type: schema.TypeString,
Required: true,
Description: "A fully qualified domain name",
ValidateFunc: validateName,
ValidateFunc: utils.ValidateName,
},
// Computed resource
"account_id": {
Expand Down
4 changes: 2 additions & 2 deletions civo/resource_dns_domain_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"log"
"strings"

"github.com/civo/terraform-provider-civo/internal/utils"
"github.com/civo/civogo"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
Expand Down Expand Up @@ -228,7 +228,7 @@ func resourceDNSDomainRecordDelete(d *schema.ResourceData, m interface{}) error
func resourceDNSDomainRecordImport(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
apiClient := m.(*civogo.Client)

domainID, DomainRecordID, err := resourceCommonParseID(d.Id())
domainID, DomainRecordID, err := utils.ResourceCommonParseID(d.Id())
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion civo/resource_firewall.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"

"github.com/civo/civogo"
"github.com/civo/terraform-provider-civo/internal/utils"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

Expand All @@ -16,7 +17,7 @@ func resourceFirewall() *schema.Resource {
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateName,
ValidateFunc: utils.ValidateName,
},
"region": {
Type: schema.TypeString,
Expand Down
5 changes: 3 additions & 2 deletions civo/resource_firewall_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"

"github.com/civo/civogo"
"github.com/civo/terraform-provider-civo/internal/utils"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
Expand All @@ -20,7 +21,7 @@ func resourceFirewallRule() *schema.Resource {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateName,
ValidateFunc: utils.ValidateName,
},
"protocol": {
Type: schema.TypeString,
Expand Down Expand Up @@ -207,7 +208,7 @@ func resourceFirewallRuleImport(d *schema.ResourceData, m interface{}) ([]*schem
apiClient.Region = region.(string)
}

firewallID, firewallRuleID, err := resourceCommonParseID(d.Id())
firewallID, firewallRuleID, err := utils.ResourceCommonParseID(d.Id())
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions civo/resource_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ func resourceInstance() *schema.Resource {
Computed: true,
Description: "A fully qualified domain name that should be set as the instance's hostname (required)",
ForceNew: true,
ValidateFunc: validateName,
ValidateFunc: utils.ValidateNameSize,
},
"reverse_dns": {
Type: schema.TypeString,
Optional: true,
Description: "A fully qualified domain name that should be used as the instance's IP's reverse DNS (optional, uses the hostname if unspecified)",
ValidateFunc: validateName,
ValidateFunc: utils.ValidateName,
},
"size": {
Type: schema.TypeString,
Expand Down Expand Up @@ -253,7 +253,7 @@ func resourceInstanceCreate(d *schema.ResourceData, m interface{}) error {
Timeout: 60 * time.Minute,
Delay: 3 * time.Second,
MinTimeout: 3 * time.Second,
NotFoundChecks: 10,
NotFoundChecks: 60,
}
_, err = createStateConf.WaitForStateContext(context.Background())
if err != nil {
Expand Down
9 changes: 6 additions & 3 deletions civo/resource_kubernetes_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/civo/terraform-provider-civo/internal/utils"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

// Kubernetes Cluster resource, with this you can manage all cluster from terraform
Expand All @@ -23,7 +22,7 @@ func resourceKubernetesCluster() *schema.Resource {
Optional: true,
Computed: true,
Description: "a name for your cluster, must be unique within your account (required)",
ValidateFunc: validation.StringIsNotEmpty,
ValidateFunc: utils.ValidateNameSize,
},
"region": {
Type: schema.TypeString,
Expand Down Expand Up @@ -270,7 +269,11 @@ func resourceKubernetesClusterCreate(d *schema.ResourceData, m interface{}) erro
}

if attr, ok := d.GetOk("applications"); ok {
config.Applications = attr.(string)
if utils.CheckAPPName(attr.(string), apiClient) {
config.Applications = attr.(string)
} else {
return fmt.Errorf("[ERR] the app that tries to install is not valid: %s", attr.(string))
}
} else {
config.Applications = ""
}
Expand Down
3 changes: 2 additions & 1 deletion civo/resource_kubernetes_cluster_nodepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
// "time"

"github.com/civo/civogo"
"github.com/civo/terraform-provider-civo/internal/utils"
"github.com/google/uuid"

// "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
Expand Down Expand Up @@ -205,7 +206,7 @@ func resourceKubernetesClusterNodePoolDelete(d *schema.ResourceData, m interface
func resourceKubernetesClusterNodePoolImport(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
apiClient := m.(*civogo.Client)

clusterID, nodePoolID, err := resourceCommonParseID(d.Id())
clusterID, nodePoolID, err := utils.ResourceCommonParseID(d.Id())
if err != nil {
return nil, err
}
Expand Down
5 changes: 3 additions & 2 deletions civo/resource_loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"

"github.com/civo/civogo"
"github.com/civo/terraform-provider-civo/internal/utils"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
Expand All @@ -18,7 +19,7 @@ func resourceLoadBalancer() *schema.Resource {
Type: schema.TypeString,
Required: true,
Description: "the hostname to receive traffic for, e.g. www.example.com (optional: sets hostname to loadbalancer-uuid.civo.com if blank)",
ValidateFunc: validateName,
ValidateFunc: utils.ValidateName,
},
"protocol": {
Type: schema.TypeString,
Expand Down Expand Up @@ -96,7 +97,7 @@ func resourceLoadBalancer() *schema.Resource {
"instance_id": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateName,
ValidateFunc: utils.ValidateName,
},
"protocol": {
Type: schema.TypeString,
Expand Down
3 changes: 2 additions & 1 deletion civo/resource_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"

"github.com/civo/civogo"
"github.com/civo/terraform-provider-civo/internal/utils"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

Expand All @@ -17,7 +18,7 @@ func resourceNetwork() *schema.Resource {
Type: schema.TypeString,
Required: true,
Description: "Name for the network",
ValidateFunc: validateName,
ValidateFunc: utils.ValidateName,
},
"region": {
Type: schema.TypeString,
Expand Down
3 changes: 2 additions & 1 deletion civo/resource_ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"

"github.com/civo/civogo"
"github.com/civo/terraform-provider-civo/internal/utils"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

Expand All @@ -17,7 +18,7 @@ func resourceSSHKey() *schema.Resource {
Type: schema.TypeString,
Required: true,
Description: "a string that will be the reference for the SSH key.",
ValidateFunc: validateName,
ValidateFunc: utils.ValidateName,
},
"public_key": {
Type: schema.TypeString,
Expand Down
3 changes: 2 additions & 1 deletion civo/resource_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/civo/civogo"
"github.com/civo/terraform-provider-civo/internal/utils"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

Expand All @@ -18,7 +19,7 @@ func resourceVolume() *schema.Resource {
Type: schema.TypeString,
Required: true,
Description: "A name that you wish to use to refer to this volume",
ValidateFunc: validateName,
ValidateFunc: utils.ValidateName,
},
"size_gb": {
Type: schema.TypeInt,
Expand Down
34 changes: 0 additions & 34 deletions civo/utils.go

This file was deleted.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module github.com/civo/terraform-provider-civo

require (
github.com/aws/aws-sdk-go v1.29.22 // indirect
github.com/civo/civogo v0.2.47
github.com/civo/civogo v0.2.49
github.com/fatih/color v1.9.0 // indirect
github.com/google/uuid v1.2.0
github.com/gorhill/cronexpr v0.0.0-20180427100037-88b0669f7d75
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ github.com/civo/civogo v0.2.45 h1:JXqMhjxfZ75mSs0aQIzvLBOQ3M6EBuInH73aIupzJRI=
github.com/civo/civogo v0.2.45/go.mod h1:SR0ZOhABfQHjgNQE3UyfX4gaYsrfslkPFRFMx5P29rg=
github.com/civo/civogo v0.2.47 h1:qMzlm75hYOfuuvB0BXBaWhe/073TxzMNpYWkNKb5JR8=
github.com/civo/civogo v0.2.47/go.mod h1:SR0ZOhABfQHjgNQE3UyfX4gaYsrfslkPFRFMx5P29rg=
github.com/civo/civogo v0.2.49 h1:tLIzbUIh3Za3Xc+dwR6IUrP1eF66P7N91kIe0S/jDhw=
github.com/civo/civogo v0.2.49/go.mod h1:SR0ZOhABfQHjgNQE3UyfX4gaYsrfslkPFRFMx5P29rg=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
Expand Down
77 changes: 77 additions & 0 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package utils

// ValidateNameSize is a functo check the size of a name
// func ValidateNameSize

import (
"fmt"
"regexp"
"strings"

"github.com/civo/civogo"
)

func ValidateName(v interface{}, k string) (ws []string, es []error) {
var errs []error
var warns []string
value, ok := v.(string)
if !ok {
errs = append(errs, fmt.Errorf("expected name to be string"))
return warns, errs
}
whiteSpace := regexp.MustCompile(`\s+`)
if whiteSpace.Match([]byte(value)) {
errs = append(errs, fmt.Errorf("name cannot contain whitespace. Got %s", value))
return warns, errs
}
return warns, errs
}

func ValidateNameSize(v interface{}, k string) (ws []string, es []error) {
var errs []error
var warns []string
value, ok := v.(string)
if !ok {
errs = append(errs, fmt.Errorf("expected name to be string"))
return warns, errs
}
whiteSpace := regexp.MustCompile(`\s+`)
if whiteSpace.Match([]byte(value)) {
errs = append(errs, fmt.Errorf("name cannot contain whitespace. Got %s", value))
return warns, errs
}

if len(value) > 36 {
errs = append(errs, fmt.Errorf("the len of the name has to be less than 36. Got %d", len(value)))
return warns, errs
}

return warns, errs
}

// util function to help the import function
func ResourceCommonParseID(id string) (string, string, error) {
parts := strings.SplitN(id, ":", 2)

if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
return "", "", fmt.Errorf("unexpected format of ID (%s), expected attribute1:attribute2", id)
}

return parts[0], parts[1], nil
}

// CheckAPPName is a function to check if the app name is valid
func CheckAPPName(appName string, client *civogo.Client) bool {
allAPP, err := client.ListKubernetesMarketplaceApplications()
if err != nil {
return false
}

for _, v := range allAPP {
if strings.Contains(appName, v.Name) {
return true
}
}

return false
}

0 comments on commit 4e43800

Please sign in to comment.