terraform-provider-greenhost/digitalocean/resource_digitalocean_conta...

95 lines
2.5 KiB
Go
Raw Normal View History

adding Container Registry support (#383) * adding Container Registry support * adding endpoint and docker_credentials as attributes to datasource * updating documentation on how to use the endpoint attribute * adding a server_url attribute * adding tests, exporting attributes from resources, fixing go.sum, updating docs * adding update function and splitting out generateDockerCreds into a separate function * updating acceptance tests and docs * Update website/docs/d/container_registry.html.md Co-Authored-By: Andrew Starr-Bochicchio <andrewsomething@users.noreply.github.com> * Update website/docs/d/container_registry.html.md Co-Authored-By: Andrew Starr-Bochicchio <andrewsomething@users.noreply.github.com> * Update website/docs/d/container_registry.html.md Co-Authored-By: Andrew Starr-Bochicchio <andrewsomething@users.noreply.github.com> * adding kubernetes example * adding note about container registry beta * removing TODOs * partial push * almost done, odd error in import test and waiting for final default value * setting default for expiry_seconds and creating custom importer * creating new resource for docker credentials * fixed tiny bug in testing * Update website/docs/r/container_registry_docker_credentials.html.markdown Co-authored-by: Andrew Starr-Bochicchio <andrewsomething@users.noreply.github.com> * Update website/docs/r/container_registry_docker_credentials.html.markdown Co-authored-by: Andrew Starr-Bochicchio <andrewsomething@users.noreply.github.com> * Final tweaks as requested Co-authored-by: Andrew Starr-Bochicchio <andrewsomething@users.noreply.github.com>
2020-06-03 21:30:15 +00:00
package digitalocean
import (
"context"
"fmt"
"log"
"github.com/digitalocean/godo"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
)
func resourceDigitalOceanContainerRegistry() *schema.Resource {
return &schema.Resource{
Create: resourceDigitalOceanContainerRegistryCreate,
Read: resourceDigitalOceanContainerRegistryRead,
Delete: resourceDigitalOceanContainerRegistryDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.NoZeroValues,
},
"endpoint": {
Type: schema.TypeString,
Computed: true,
},
"server_url": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceDigitalOceanContainerRegistryCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*CombinedConfig).godoClient()
// Build up our creation options
opts := &godo.RegistryCreateRequest{
Name: d.Get("name").(string),
}
log.Printf("[DEBUG] Container Registry create configuration: %#v", opts)
reg, _, err := client.Registry.Create(context.Background(), opts)
if err != nil {
return fmt.Errorf("Error creating container registry: %s", err)
}
d.SetId(reg.Name)
log.Printf("[INFO] Container Registry: %s", reg.Name)
return resourceDigitalOceanContainerRegistryRead(d, meta)
}
func resourceDigitalOceanContainerRegistryRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*CombinedConfig).godoClient()
reg, resp, err := client.Registry.Get(context.Background())
if err != nil {
// If the registry is somehow already destroyed, mark as
// successfully gone
if resp != nil && resp.StatusCode == 404 {
d.SetId("")
return nil
}
return fmt.Errorf("Error retrieving container registry: %s", err)
}
d.SetId(reg.Name)
d.Set("name", reg.Name)
d.Set("endpoint", fmt.Sprintf("%s/%s", RegistryHostname, reg.Name))
d.Set("server_url", RegistryHostname)
return nil
}
func resourceDigitalOceanContainerRegistryDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*CombinedConfig).godoClient()
log.Printf("[INFO] Deleting container registry: %s", d.Id())
_, err := client.Registry.Delete(context.Background())
if err != nil {
return fmt.Errorf("Error deleting container registry: %s", err)
}
d.SetId("")
return nil
}