Skip to content

Commit

Permalink
Bump civogo to v0.2.57 and disable loadbalancer, template and snapsho…
Browse files Browse the repository at this point in the history
…t files (#105)
  • Loading branch information
zulh-civo committed Nov 9, 2021
1 parent 4900084 commit c558d81
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 19 deletions.
90 changes: 85 additions & 5 deletions civo/datasource_disk_image.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
package civo

import (
"fmt"

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

// TemplateDisk is a temporal struct to get all template in one place
type TemplateDisk struct {
ID string
Name string
Version string
Label string
}

// Data source to get from the api a specific template
// using the code of the image
func dataSourceDiskImage() *schema.Resource {

dataListConfig := &datalist.ResourceConfig{
RecordSchema: templateSchema(),
RecordSchema: diskimageSchema(),
Description: "Get information on an disk image for use in other resources (e.g. creating a instance) with the ability to filter the results.",
ExtraQuerySchema: map[string]*schema.Schema{
"region": {
Expand All @@ -20,11 +31,80 @@ func dataSourceDiskImage() *schema.Resource {
},
},
ResultAttributeName: "diskimages",

// use functions from "datasource_template.go" file
FlattenRecord: flattenTemplate,
GetRecords: getTemplates,
FlattenRecord: flattenDiskimage,
GetRecords: getDiskimages,
}

return datalist.NewResource(dataListConfig)
}

func getDiskimages(m interface{}, extra map[string]interface{}) ([]interface{}, error) {
apiClient := m.(*civogo.Client)

// overwrite the region if is define in the datasource
region, ok := extra["region"].(string)
if !ok {
return nil, fmt.Errorf("unable to find `region` key from query data")
}

if region != "" {
apiClient.Region = region
}

templateDiskList := []TemplateDisk{}

diskImage, err := apiClient.ListDiskImages()
if err != nil {
return nil, fmt.Errorf("[ERR] error retrieving all Disk Images: %s", err)
}

for _, v := range diskImage {
templateDiskList = append(templateDiskList, TemplateDisk{ID: v.ID, Name: v.Name, Version: v.Version, Label: v.Label})
}

templates := []interface{}{}
for _, partialSize := range templateDiskList {
templates = append(templates, partialSize)
}

return templates, nil
}

func flattenDiskimage(template, m interface{}, extra map[string]interface{}) (map[string]interface{}, error) {

s := template.(TemplateDisk)

flattenedTemplate := map[string]interface{}{}
flattenedTemplate["id"] = s.ID
flattenedTemplate["name"] = s.Name
flattenedTemplate["version"] = s.Version
flattenedTemplate["label"] = s.Label

return flattenedTemplate, nil
}

func diskimageSchema() map[string]*schema.Schema {

return map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: "ID of disk image",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "Name of disk image",
},
"version": {
Type: schema.TypeString,
Computed: true,
Description: "Version of disk image",
},
"label": {
Type: schema.TypeString,
Computed: true,
Description: "Label of disk image",
},
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion civo/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func Provider() *schema.Provider {
},
},
DataSourcesMap: map[string]*schema.Resource{
"civo_template": dataSourceTemplate(),
// "civo_template": dataSourceTemplate(),
"civo_disk_image": dataSourceDiskImage(),
"civo_kubernetes_version": dataSourceKubernetesVersion(),
"civo_kubernetes_cluster": dataSourceKubernetesCluster(),
Expand Down
16 changes: 4 additions & 12 deletions civo/resource_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,19 +215,11 @@ func resourceInstanceCreate(d *schema.ResourceData, m interface{}) error {

if attr, ok := d.GetOk("template"); ok {
templateID := ""
if apiClient.Region == "SVG1" {
findTemplate, err := apiClient.FindTemplate(attr.(string))
if err != nil {
return fmt.Errorf("[ERR] failed to get the template: %s", err)
}
templateID = findTemplate.ID
} else {
findTemplate, err := apiClient.FindDiskImage(attr.(string))
if err != nil {
return fmt.Errorf("[ERR] failed to get the template: %s", err)
}
templateID = findTemplate.ID
findTemplate, err := apiClient.FindDiskImage(attr.(string))
if err != nil {
return fmt.Errorf("[ERR] failed to get the template: %s", err)
}
templateID = findTemplate.ID
config.TemplateID = templateID
}

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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.52
github.com/civo/civogo v0.2.57
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 @@ -67,6 +67,8 @@ github.com/civo/civogo v0.2.49 h1:tLIzbUIh3Za3Xc+dwR6IUrP1eF66P7N91kIe0S/jDhw=
github.com/civo/civogo v0.2.49/go.mod h1:SR0ZOhABfQHjgNQE3UyfX4gaYsrfslkPFRFMx5P29rg=
github.com/civo/civogo v0.2.52 h1:oeMmeGuJOZFJ+uruu13ywCWOxSa2+Lyk+ePc6rxC8gY=
github.com/civo/civogo v0.2.52/go.mod h1:SR0ZOhABfQHjgNQE3UyfX4gaYsrfslkPFRFMx5P29rg=
github.com/civo/civogo v0.2.57 h1:wdZQeivTk/vFaeJBQdFLZobsK6qE/Pb3XR3fxk5DCZU=
github.com/civo/civogo v0.2.57/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

0 comments on commit c558d81

Please sign in to comment.