-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a9ee99e
commit e9a6e02
Showing
13 changed files
with
876 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
terraform-provider-dockerhub: *.go */*.go go.mod | ||
go build . | ||
|
||
install: terraform-provider-dockerhub | ||
mkdir -p ~/.terraform.d/plugins/registry.terraform.io/BarnabyShearer/dockerhub/0.1.0/linux_amd64 | ||
cp $+ ~/.terraform.d/plugins/registry.terraform.io/BarnabyShearer/dockerhub/0.1.0/linux_amd64 | ||
-rm .terraform.lock.hcl | ||
terraform init |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
# terraform-provider-dockerhub | ||
|
||
[![registry.terraform.io](https://img.shields.io/badge/terraform-docs-success)](https://registry.terraform.io/providers/BarnabyShearer/dockerhub/latest/docs) | ||
|
||
Register hub.docker.io repositories. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package dockerhub | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
|
||
rtd "github.com/BarnabyShearer/dockerhub/v2" | ||
) | ||
|
||
func Provider() *schema.Provider { | ||
return &schema.Provider{ | ||
Schema: map[string]*schema.Schema{ | ||
"username": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
DefaultFunc: schema.EnvDefaultFunc("DOCKER_USERNAME", nil), | ||
Description: "Username for authentication.", | ||
}, | ||
"password": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
DefaultFunc: schema.EnvDefaultFunc("DOCKER_PASSWORD", nil), | ||
Description: "Password for authentication.", | ||
}, | ||
}, | ||
ResourcesMap: map[string]*schema.Resource{ | ||
"dockerhub_repository": resourceRepository(), | ||
"dockerhub_token": resourceToken(), | ||
}, | ||
ConfigureContextFunc: providerConfigure, | ||
} | ||
} | ||
|
||
func providerConfigure(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) { | ||
return rtd.NewClient(d.Get("username").(string), d.Get("password").(string)), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package dockerhub | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
|
||
dh "github.com/BarnabyShearer/dockerhub/v2" | ||
) | ||
|
||
func resourceRepository() *schema.Resource { | ||
return &schema.Resource{ | ||
Description: "A hub.docker.io repository.", | ||
CreateContext: resourceRepositoryCreate, | ||
UpdateContext: resourceRepositoryUpdate, | ||
ReadContext: resourceRepositoryRead, | ||
DeleteContext: resourceRepositoryDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
Schema: map[string]*schema.Schema{ | ||
"id": { | ||
Description: "The namespace/name of the repository.", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"namespace": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "Repository namespace.", | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "Repository name.", | ||
}, | ||
"description": { | ||
Type: schema.TypeString, | ||
Required: false, | ||
Description: "Repository name.", | ||
}, | ||
"full_description": { | ||
Type: schema.TypeString, | ||
Required: false, | ||
Description: "Repository name.", | ||
}, | ||
"private": { | ||
Type: schema.TypeBool, | ||
Required: false, | ||
Default: false, | ||
Description: "Is the repository private.", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func updateReqest(d *schema.ResourceData) dh.Repository { | ||
return dh.Repository{ | ||
Name: d.Get("name").(string), | ||
Description: d.Get("description").(string), | ||
FullDescription: d.Get("full_description").(string), | ||
Private: d.Get("private").(bool), | ||
} | ||
} | ||
|
||
func resourceRepositoryCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
client := m.(*dh.Client) | ||
repository, err := client.CreateRepository(ctx, updateReqest(d)) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
d.SetId(fmt.Sprintf("%s/%s", repository.Namespace, repository.Name)) | ||
return nil | ||
} | ||
|
||
func resourceRepositoryRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
client := m.(*dh.Client) | ||
repository, err := client.GetRepository(ctx, d.Id()) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
d.Set("description", repository.Description) | ||
d.Set("full_description", repository.FullDescription) | ||
d.Set("private", repository.Private) | ||
return nil | ||
} | ||
|
||
func resourceRepositoryUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
client := m.(*dh.Client) | ||
err := client.UpdateRepository(ctx, d.Id(), updateReqest(d)) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
return nil | ||
} | ||
|
||
func resourceRepositoryDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
client := m.(*dh.Client) | ||
err := client.DeleteRepository(ctx, d.Id()) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
d.SetId("") | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package dockerhub | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
|
||
dh "github.com/BarnabyShearer/dockerhub/v2" | ||
) | ||
|
||
func resourceToken() *schema.Resource { | ||
return &schema.Resource{ | ||
Description: "A hub.docker.io personal access token (for uploading images).", | ||
CreateContext: resourceTokenCreate, | ||
DeleteContext: resourceTokenDelete, | ||
Schema: map[string]*schema.Schema{ | ||
"id": { | ||
Description: "The UUID of the token.", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"label": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "Token label.", | ||
}, | ||
"token": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Sensitive: true, | ||
Description: "Token to use as password", | ||
}, | ||
"scopes": { | ||
Type: schema.TypeList, | ||
Required: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceTokenCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
client := m.(*dh.Client) | ||
token, err := client.CreatePersonalAccessToken(ctx, dh.CreatePersonalAccessToken{ | ||
TokenLabel: d.Get("label").(string), | ||
Scopes: d.Get("scopes").([]string), | ||
}) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
d.SetId(token.UUID) | ||
d.Set("token", token.Token) | ||
return nil | ||
} | ||
|
||
func resourceTokenDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
client := m.(*dh.Client) | ||
err := client.DeletePersonalAccessToken(ctx, d.Id()) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
d.SetId("") | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
terraform { | ||
required_providers { | ||
dockerhub = { | ||
source = "BarnabyShearer/dockerhub" | ||
} | ||
} | ||
} | ||
|
||
provider "dockerhub" { | ||
# Note this cannot be a Personal Access Token | ||
username = "USERNAME" # optionally use DOCKER_USERNAME env var | ||
password = "PASSWORD" # optionally use DOCKER_PASSWORD env var | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# import using the namespace/name | ||
terraform import dockerhub_repository example username/example |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
resource "dockerhub_repository" "example" { | ||
name = "example" | ||
description = "Example repository." | ||
full_description = "Readme." | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
resource "dockerhub_token" "example" { | ||
label = "example" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module github.com/BarnabyShearer/terraform-provider-dockerhub | ||
|
||
go 1.16 | ||
|
||
require ( | ||
github.com/BarnabyShearer/dockerhub/v2 v2.0.1 | ||
github.com/hashicorp/terraform-plugin-docs v0.5.1 | ||
github.com/hashicorp/terraform-plugin-sdk/v2 v2.9.0 | ||
) |
Oops, something went wrong.