Skip to content

Commit

Permalink
feat: Added the option to import existing infrastructure
Browse files Browse the repository at this point in the history
- Update all comment in all components
- Added the option to import existing infrastructure
- Update civogo lib to 0.2.5

BREAKING CHANGE: No

Signed-off-by: Alejandro JNM <alejandrojnm@gmail.com>
  • Loading branch information
alejandrojnm committed Apr 15, 2020
1 parent 77c3d35 commit e4dc011
Show file tree
Hide file tree
Showing 21 changed files with 399 additions and 139 deletions.
1 change: 1 addition & 0 deletions civo/datasource_common_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

// Function to define a custom schema filter for data source
func dataSourceFiltersSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeSet,
Expand Down
1 change: 1 addition & 0 deletions civo/datasource_common_struct.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package civo

// Customs struct for Filter used in dataSourceFiltersSchema
type Filter struct {
Name string
Values []string
Expand Down
9 changes: 7 additions & 2 deletions civo/datasource_intances_size.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import (
"fmt"
"github.com/civo/civogo"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"log"
"regexp"
"strconv"
)

// Data source to get and filter all instances size
// use to define the size in resourceInstance
func dataSourceInstancesSize() *schema.Resource {
return &schema.Resource{
Read: dataSourceInstancesSizeRead,
Expand Down Expand Up @@ -56,11 +59,13 @@ func dataSourceInstancesSizeRead(d *schema.ResourceData, m interface{}) error {
}

if filtersOk {
log.Printf("[INFO] Getting the instances size")
resp, err := apiClient.ListInstanceSizes()
if err != nil {
return fmt.Errorf("no instances size was found in the server")
}

log.Printf("[INFO] Finding the size of the instances")
size, err := findInstancesSizeByFilter(resp, filters.(*schema.Set))
if err != nil {
return fmt.Errorf("no instances size was found in the server, %s", err)
Expand Down Expand Up @@ -159,7 +164,7 @@ func findInstancesSizeByFilter(sizes []civogo.InstanceSize, set *schema.Set) (*c
return &results[0], nil
}
if len(results) == 0 {
return nil, fmt.Errorf("no sizes found for your search")
return nil, fmt.Errorf("no instances sizes found for your search")
}
return nil, fmt.Errorf("too many sizes found (found %d, expected 1)", len(results))
return nil, fmt.Errorf("too many instances sizes found (found %d, expected 1)", len(results))
}
10 changes: 8 additions & 2 deletions civo/datasource_kubernetes_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import (
"fmt"
"github.com/civo/civogo"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"log"
)

// Data source to get and filter all kubernetes version
// available in the server, use to define the version at the
// moment of the cluster creation in resourceKubernetesCluster
func dataSourceKubernetesVersion() *schema.Resource {
return &schema.Resource{
Read: dataSourceKubernetesVersionRead,
Expand Down Expand Up @@ -42,11 +46,13 @@ func dataSourceKubernetesVersionRead(d *schema.ResourceData, m interface{}) erro
}

if filtersOk {
log.Printf("[INFO] Getting all versions of kubernetes")
resp, err := apiClient.ListAvailableKubernetesVersions()
if err != nil {
return fmt.Errorf("no version was found in the server")
}

log.Printf("[INFO] Finding the version of kubernetes")
version, err := findKubernetesVersionByFilter(resp, filters.(*schema.Set))
if err != nil {
return fmt.Errorf("no version was found in the server, %s", err)
Expand Down Expand Up @@ -99,7 +105,7 @@ func findKubernetesVersionByFilter(version []civogo.KubernetesVersion, set *sche
return &results[0], nil
}
if len(results) == 0 {
return nil, fmt.Errorf("no version found for your search")
return nil, fmt.Errorf("no kubernetes version found for your search")
}
return nil, fmt.Errorf("too many version found (found %d, expected 1)", len(results))
return nil, fmt.Errorf("too many kubernetes version found (found %d, expected 1)", len(results))
}
4 changes: 4 additions & 0 deletions civo/datasource_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import (
"github.com/civo/civogo"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"log"
)

// Data source to get from the api a specific template
// using the code of the image
func dataSourceTemplate() *schema.Resource {
return &schema.Resource{
Read: dataSourceTemplateRead,
Expand Down Expand Up @@ -68,6 +71,7 @@ func dataSourceTemplateRead(d *schema.ResourceData, m interface{}) error {
}

if hasCode {
log.Printf("[INFO] Getting all template")
image, err := apiClient.GetTemplateByCode(code.(string))
if err != nil {
fmt.Errorf("[ERR] failed to retrive template: %s", err)
Expand Down
2 changes: 2 additions & 0 deletions civo/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)

// Civo cloud provider
func Provider() terraform.ResourceProvider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -38,6 +39,7 @@ func Provider() terraform.ResourceProvider {
}
}

// Provider configuration
func providerConfigure(d *schema.ResourceData) (interface{}, error) {
token := d.Get("token").(string)
client, _ := civogo.NewClient(token)
Expand Down
42 changes: 36 additions & 6 deletions civo/resource_dns_domain_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log"
)

// Dns Domain resource, with this we can create and manage DNS Domain
func resourceDnsDomainName() *schema.Resource {
fmt.Print()
return &schema.Resource{
Expand All @@ -29,14 +30,16 @@ func resourceDnsDomainName() *schema.Resource {
Delete: resourceDnsDomainNameDelete,
//Exists: resourceExistsItem,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
State: resourceDnsDomainImport,
},
}
}

// function to create a new domain in your account
func resourceDnsDomainNameCreate(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*civogo.Client)

log.Printf("[INFO] Creating the domain %s", d.Get("name").(string))
dnsDomain, err := apiClient.CreateDNSDomain(d.Get("name").(string))
if err != nil {
fmt.Errorf("failed to create a new domains: %s", err)
Expand All @@ -48,17 +51,19 @@ func resourceDnsDomainNameCreate(d *schema.ResourceData, m interface{}) error {
return resourceDnsDomainNameRead(d, m)
}

// function to read a domain from your account
func resourceDnsDomainNameRead(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*civogo.Client)

log.Printf("[INFO] retriving the domain %s", d.Get("name").(string))
resp, err := apiClient.GetDNSDomain(d.Get("name").(string))
if err != nil {
if resp != nil {
d.SetId("")
return nil
}

return fmt.Errorf("error retrieving domain: %s", err)
return fmt.Errorf("[ERR] error retrieving domain: %s", err)
}

d.Set("name", resp.Name)
Expand All @@ -67,41 +72,66 @@ func resourceDnsDomainNameRead(d *schema.ResourceData, m interface{}) error {
return nil
}

// function to update a specific domain
func resourceDnsDomainNameUpdate(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*civogo.Client)

log.Printf("[INFO] Searching the domain %s", d.Get("name").(string))
resp, err := apiClient.FindDNSDomain(d.Id())
if err != nil {
log.Printf("[WARN] Civo domain (%s) not found", d.Id())
log.Printf("[WARN] domain (%s) not found", d.Id())
d.SetId("")
return nil
}

if d.HasChange("name") {
name := d.Get("name").(string)
log.Printf("[INFO] Renaming the domain to %s", d.Get("name").(string))
_, err := apiClient.UpdateDNSDomain(resp, name)
if err != nil {
log.Printf("[WARN] An error occurred while renamed the domain (%s)", d.Id())
return fmt.Errorf("[WARN] an error occurred while renamed the domain (%s)", d.Id())
}

}

return resourceDnsDomainNameRead(d, m)
}

// function to delete a specific domain
func resourceDnsDomainNameDelete(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*civogo.Client)

log.Printf("[INFO] Searching the domain to %s", d.Get("name").(string))
resp, err := apiClient.FindDNSDomain(d.Id())
if err != nil {
log.Printf("[WARN] Civo domain (%s) not found", d.Id())
log.Printf("[WARN] domain (%s) not found", d.Id())
d.SetId("")
return nil
}

log.Printf("[INFO] Deleting the domain %s", d.Get("name").(string))
_, err = apiClient.DeleteDNSDomain(resp)
if err != nil {
log.Printf("[INFO] Civo domain (%s) was delete", d.Id())
return fmt.Errorf("[ERR] an error occurred while tring to delete the domain %s", d.Id())
}
return nil
}

// custom import to able add a main domain to the terraform
func resourceDnsDomainImport(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
apiClient := m.(*civogo.Client)

log.Printf("[INFO] Searching the domain %s", d.Get("name").(string))
resp, err := apiClient.GetDNSDomain(d.Id())
if err != nil {
if resp != nil {
return nil, err
}
}

d.SetId(resp.ID)
d.Set("name", resp.Name)
d.Set("account_id", resp.AccountID)

return []*schema.ResourceData{d}, nil
}
Loading

0 comments on commit e4dc011

Please sign in to comment.