Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
BarnabyShearer committed Nov 22, 2021
1 parent a9ee99e commit e9a6e02
Show file tree
Hide file tree
Showing 13 changed files with 876 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Makefile
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
4 changes: 4 additions & 0 deletions README.md
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.

38 changes: 38 additions & 0 deletions dockerhub/provider.go
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
}
107 changes: 107 additions & 0 deletions dockerhub/resource_repository.go
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
}
64 changes: 64 additions & 0 deletions dockerhub/resource_token.go
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
}
13 changes: 13 additions & 0 deletions examples/provider/provider.tf
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
}
2 changes: 2 additions & 0 deletions examples/resources/dockerhub_repository/import.sh
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
5 changes: 5 additions & 0 deletions examples/resources/dockerhub_repository/resource.tf
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."
}
3 changes: 3 additions & 0 deletions examples/resources/dockerhub_token/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
resource "dockerhub_token" "example" {
label = "example"
}
9 changes: 9 additions & 0 deletions go.mod
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
)
Loading

0 comments on commit e9a6e02

Please sign in to comment.