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"
upgrade provider to use terraform-plugin-sdk v2 (#492) * upgrade terraform-plugin-sdk and `go mod vendor` * Update digitalocean/datasource_digitalocean_image_test.go Co-authored-by: Andrew Starr-Bochicchio <andrewsomething@users.noreply.github.com> * Update digitalocean/datasource_digitalocean_kubernetes_cluster_test.go Co-authored-by: Andrew Starr-Bochicchio <andrewsomething@users.noreply.github.com> * Update digitalocean/datasource_digitalocean_vpc_test.go Co-authored-by: Andrew Starr-Bochicchio <andrewsomething@users.noreply.github.com> * Update digitalocean/datasource_digitalocean_vpc_test.go Co-authored-by: Andrew Starr-Bochicchio <andrewsomething@users.noreply.github.com> * go fmt * fix droplet_id to be of the right type * fix digitalocean_project resource * fix creation order in digitalocean_certificate test * fix digitalocean_container_registry data source tes * Port new changes to v2. * Port all tests to resource.ParallelTest * Fix KubernetesProviderInteroperability test. * Fix TestAccDigitalOceanKubernetesCluster_UpgradeVersion * Fix firewall panic s/create_at/created_at/ * Fix TestAccDigitalOceanDroplet_Basic: Droplets now have private networking by default. * Fix TestAccDataSourceDigitalOceanDomain_Basic * Fix TestAccDataSourceDigitalOceanDropletSnapshot tests. * Fix TestAccDataSourceDigitalOceanSSHKey_Basic * Fix TestAccDataSourceDigitalOceanVolumeSnapshot tests. * Fix TestAccDataSourceDigitalOceanVolume tests. * Fix TestAccDataSourceDigitalOceanRecord_Basic * Fix TestAccDataSourceDigitalOceanProject_NonDefaultProject * Fix TestAccDigitalOceanImage_PublicSlug * Fix TestAccDataSourceDigitalOceanImages_Basic via bug in imageSchema() * go mod tidy * Fix TestAccDataSourceDigitalOceanDroplet tests. * Fix TestAccDataSourceDigitalOceanVPC_ByName * Fix TestAccDataSourceDigitalOceanTag_Basic * Fix TestAccDataSourceDigitalOceanTags_Basic * Ensure versions are set in DBaaS tests. * Fix TestAccDataSourceDigitalOceanApp_Basic * Fix non-set related issues with TestAccDataSourceDigitalOceanLoadBalancer tests. * Fix TestAccDataSourceDigitalOceanKubernetesCluster_Basic * Remove testAccDigitalOceanKubernetesConfigWithEmptyNodePool: Empty node pools are no longer supported. * Fix TestAccDigitalOceanProject_WithManyResources. * Fix TestAccDigitalOceanProject_UpdateFromDropletToSpacesResource * vendor set helpers from AWS provider * Fix TestAccDigitalOceanFloatingIP_Droplet. * Fix CDN panic. * fix TestAccDigitalOceanSpacesBucket_LifecycleBasic using setutil helpers * vendor set helpers from AWS provider * fix TestAccDigitalOceanSpacesBucket_LifecycleBasic using setutil helpers * Fix load balancer tests using setutil helpers. * Fix K8s tests using setutil helpers. * Fix TestAccDigitalOceanApp_Envs using setutil helpers. * Fix TestAccDigitalOceanSpacesBucket_LifecycleExpireMarkerOnly using setutil helpers. * Fix TestAccDigitalOceanFloatingIPAssignment_createBeforeDestroy * fix remaining TypeSet tests using setutil * Registry test can not run in parallel. One per account. * Fix TestAccDigitalOceanProject_UpdateWithDropletResource * Fix replica tests. * go mod tidy Co-authored-by: Andrew Starr-Bochicchio <andrewsomething@users.noreply.github.com> Co-authored-by: Andrew Starr-Bochicchio <a.starr.b@gmail.com>
2020-10-16 19:50:20 +00:00
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
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
)
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
}