terraform-provider-greenhost/digitalocean/datasource_digitalocean_tag.go

73 lines
1.7 KiB
Go
Raw Normal View History

2018-09-04 11:37:12 +00:00
package digitalocean
import (
"context"
"fmt"
Upgrade to terraform-plugin-sdk (#321) * Upgrade to terraform-plugin-sdk After following the [guide](https://www.terraform.io/docs/extend/plugin-sdk.html#using-tf-sdk-migrator) Check ``` ▶ GO111MODULE=on tf-sdk-migrator check Checking Go runtime version ... Go version 1.12.10: OK. Checking whether provider uses Go modules... Go modules in use: OK. Checking version of github.com/hashicorp/terraform-plugin-sdk to determine if provider was already migrated... Checking version of github.com/hashicorp/terraform used in provider... Terraform version 0.12.8: OK. Checking whether provider uses deprecated SDK packages or identifiers... No imports of deprecated SDK packages or identifiers: OK. All constraints satisfied. Provider can be migrated to the new SDK. ``` Migrate ``` ▶ GO111MODULE=on tf-sdk-migrator migrate Checking Go runtime version ... Go version 1.12.10: OK. Checking whether provider uses Go modules... Go modules in use: OK. Checking version of github.com/hashicorp/terraform-plugin-sdk to determine if provider was already migrated... Checking version of github.com/hashicorp/terraform used in provider... Terraform version 0.12.8: OK. Checking whether provider uses deprecated SDK packages or identifiers... No imports of deprecated SDK packages or identifiers: OK. All constraints satisfied. Provider can be migrated to the new SDK. Rewriting provider go.mod file... Rewriting SDK package imports... Running `go mod tidy`... Success! Provider is migrated to github.com/hashicorp/terraform-plugin-sdk v1.1.0. It looks like this provider vendors dependencies. Don't forget to run `go mod vendor`. Make sure to review all changes and run all tests. ``` * Fix build under go 1.13.x.
2019-10-22 21:44:03 +00:00
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
2018-09-04 11:37:12 +00:00
)
func dataSourceDigitalOceanTag() *schema.Resource {
return &schema.Resource{
Read: dataSourceDigitalOceanTagRead,
Schema: map[string]*schema.Schema{
"name": {
2018-09-04 11:37:12 +00:00
Type: schema.TypeString,
Required: true,
Description: "name of the tag",
ValidateFunc: validateTag,
},
"total_resource_count": {
Type: schema.TypeInt,
Computed: true,
},
"droplets_count": {
Type: schema.TypeInt,
Computed: true,
},
"images_count": {
Type: schema.TypeInt,
Computed: true,
},
"volumes_count": {
Type: schema.TypeInt,
Computed: true,
},
"volume_snapshots_count": {
Type: schema.TypeInt,
Computed: true,
},
"databases_count": {
Type: schema.TypeInt,
Computed: true,
},
2018-09-04 11:37:12 +00:00
},
}
}
func dataSourceDigitalOceanTagRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*CombinedConfig).godoClient()
2018-09-04 11:37:12 +00:00
name := d.Get("name").(string)
tag, resp, err := client.Tags.Get(context.Background(), name)
if err != nil {
if resp != nil && resp.StatusCode == 404 {
2018-09-04 11:37:12 +00:00
return fmt.Errorf("tag not found: %s", err)
}
return fmt.Errorf("Error retrieving tag: %s", err)
}
d.SetId(tag.Name)
d.Set("name", tag.Name)
d.Set("total_resource_count", tag.Resources.Count)
d.Set("droplets_count", tag.Resources.Droplets.Count)
d.Set("images_count", tag.Resources.Images.Count)
d.Set("volumes_count", tag.Resources.Volumes.Count)
d.Set("volume_snapshots_count", tag.Resources.VolumeSnapshots.Count)
d.Set("databases_count", tag.Resources.Databases.Count)
2018-09-04 11:37:12 +00:00
return nil
}