Skip to content

Commit

Permalink
Deprecate civo_template (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
zulh-civo committed Sep 17, 2021
1 parent 8d2f4c5 commit dec823b
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 478 deletions.
28 changes: 28 additions & 0 deletions civo/datasource_disk_image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package civo

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

// 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(),
ExtraQuerySchema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
},
},
ResultAttributeName: "diskimages",

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

return datalist.NewResource(dataListConfig)
}
5 changes: 3 additions & 2 deletions civo/datasource_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ func dataSourceTemplate() *schema.Resource {
GetRecords: getTemplates,
}

return datalist.NewResource(dataListConfig)

sr := datalist.NewResource(dataListConfig)
sr.DeprecationMessage = "\"civo_template\" data source is deprecated. Moving forward, please use \"civo_disk_image\" data source."
return sr
}

func getTemplates(m interface{}, extra map[string]interface{}) ([]interface{}, error) {
Expand Down
2 changes: 1 addition & 1 deletion civo/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func Provider() *schema.Provider {
},
DataSourcesMap: map[string]*schema.Resource{
"civo_template": dataSourceTemplate(),
"civo_disk_image": dataSourceDiskImage(),
"civo_kubernetes_version": dataSourceKubernetesVersion(),
"civo_kubernetes_cluster": dataSourceKubernetesCluster(),
"civo_instances_size": dataSourceInstancesSize(),
Expand All @@ -51,7 +52,6 @@ func Provider() *schema.Provider {
"civo_firewall_rule": resourceFirewallRule(),
"civo_loadbalancer": resourceLoadBalancer(),
"civo_ssh_key": resourceSSHKey(),
"civo_template": resourceTemplate(),
"civo_snapshot": resourceSnapshot(),
"civo_kubernetes_cluster": resourceKubernetesCluster(),
"civo_kubernetes_node_pool": resourceKubernetesClusterNodePool(),
Expand Down
31 changes: 27 additions & 4 deletions civo/resource_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,19 @@ func resourceInstance() *schema.Resource {
Description: "This must be the ID of the network from the network listing (optional; default network used when not specified)",
},
"template": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The ID for the template to use to build the instance",
Type: schema.TypeString,
Optional: true,
Computed: true,
ExactlyOneOf: []string{"template", "disk_image"},
Deprecated: "\"template\" attribute is deprecated. Moving forward, please use \"disk_image\" attribute.",
Description: "The ID for the template to use to build the instance",
},
"disk_image": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ExactlyOneOf: []string{"template", "disk_image"},
Description: "The ID for the disk image to use to build the instance",
},
"initial_user": {
Type: schema.TypeString,
Expand Down Expand Up @@ -215,6 +224,16 @@ func resourceInstanceCreate(d *schema.ResourceData, m interface{}) error {
config.TemplateID = templateID
}

if attr, ok := d.GetOk("disk_image"); ok {
diskImageID := ""
findDiskImage, err := apiClient.FindDiskImage(attr.(string))
if err != nil {
return fmt.Errorf("[ERR] failed to get the disk image: %s", err)
}
diskImageID = findDiskImage.ID
config.TemplateID = diskImageID
}

if attr, ok := d.GetOk("initial_user"); ok {
config.InitialUser = attr.(string)
}
Expand Down Expand Up @@ -332,6 +351,10 @@ func resourceInstanceRead(d *schema.ResourceData, m interface{}) error {
d.Set("template", d.Get("template").(string))
}

if _, ok := d.GetOk("disk_image"); ok {
d.Set("disk_image", d.Get("disk_image").(string))
}

return nil
}

Expand Down
228 changes: 0 additions & 228 deletions civo/resource_template.go

This file was deleted.

Loading

0 comments on commit dec823b

Please sign in to comment.