github.com/hugorut/terraform@v1.1.3/src/backend/remote-state/artifactory/backend.go (about) 1 package artifactory 2 3 import ( 4 "context" 5 6 cleanhttp "github.com/hashicorp/go-cleanhttp" 7 "github.com/hugorut/terraform/src/backend" 8 "github.com/hugorut/terraform/src/legacy/helper/schema" 9 "github.com/hugorut/terraform/src/states/remote" 10 "github.com/hugorut/terraform/src/states/statemgr" 11 artifactory "github.com/lusis/go-artifactory/src/artifactory.v401" 12 ) 13 14 func New() backend.Backend { 15 s := &schema.Backend{ 16 Schema: map[string]*schema.Schema{ 17 "username": &schema.Schema{ 18 Type: schema.TypeString, 19 Required: true, 20 DefaultFunc: schema.EnvDefaultFunc("ARTIFACTORY_USERNAME", nil), 21 Description: "Username", 22 }, 23 "password": &schema.Schema{ 24 Type: schema.TypeString, 25 Required: true, 26 DefaultFunc: schema.EnvDefaultFunc("ARTIFACTORY_PASSWORD", nil), 27 Description: "Password", 28 }, 29 "url": &schema.Schema{ 30 Type: schema.TypeString, 31 Required: true, 32 DefaultFunc: schema.EnvDefaultFunc("ARTIFACTORY_URL", nil), 33 Description: "Artfactory base URL", 34 }, 35 "repo": &schema.Schema{ 36 Type: schema.TypeString, 37 Required: true, 38 Description: "The repository name", 39 }, 40 "subpath": &schema.Schema{ 41 Type: schema.TypeString, 42 Required: true, 43 Description: "Path within the repository", 44 }, 45 }, 46 } 47 48 b := &Backend{Backend: s} 49 b.Backend.ConfigureFunc = b.configure 50 return b 51 } 52 53 type Backend struct { 54 *schema.Backend 55 56 client *ArtifactoryClient 57 } 58 59 func (b *Backend) configure(ctx context.Context) error { 60 data := schema.FromContextBackendConfig(ctx) 61 62 userName := data.Get("username").(string) 63 password := data.Get("password").(string) 64 url := data.Get("url").(string) 65 repo := data.Get("repo").(string) 66 subpath := data.Get("subpath").(string) 67 68 clientConf := &artifactory.ClientConfig{ 69 BaseURL: url, 70 Username: userName, 71 Password: password, 72 Transport: cleanhttp.DefaultPooledTransport(), 73 } 74 nativeClient := artifactory.NewClient(clientConf) 75 76 b.client = &ArtifactoryClient{ 77 nativeClient: &nativeClient, 78 userName: userName, 79 password: password, 80 url: url, 81 repo: repo, 82 subpath: subpath, 83 } 84 return nil 85 } 86 87 func (b *Backend) Workspaces() ([]string, error) { 88 return nil, backend.ErrWorkspacesNotSupported 89 } 90 91 func (b *Backend) DeleteWorkspace(string) error { 92 return backend.ErrWorkspacesNotSupported 93 } 94 95 func (b *Backend) StateMgr(name string) (statemgr.Full, error) { 96 if name != backend.DefaultStateName { 97 return nil, backend.ErrWorkspacesNotSupported 98 } 99 return &remote.State{ 100 Client: b.client, 101 }, nil 102 }