github.com/federicobaldo/terraform@v0.6.15-0.20160323222747-b20f680cbf05/builtin/providers/triton/provider.go (about) 1 package triton 2 3 import ( 4 "fmt" 5 "log" 6 "os" 7 8 "github.com/hashicorp/go-multierror" 9 "github.com/hashicorp/terraform/helper/schema" 10 "github.com/hashicorp/terraform/terraform" 11 "github.com/joyent/gocommon/client" 12 "github.com/joyent/gosdc/cloudapi" 13 "github.com/joyent/gosign/auth" 14 ) 15 16 // Provider returns a terraform.ResourceProvider. 17 func Provider() terraform.ResourceProvider { 18 return &schema.Provider{ 19 Schema: map[string]*schema.Schema{ 20 "account": &schema.Schema{ 21 Type: schema.TypeString, 22 Required: true, 23 DefaultFunc: schema.EnvDefaultFunc("SDC_ACCOUNT", ""), 24 }, 25 26 "url": &schema.Schema{ 27 Type: schema.TypeString, 28 Required: true, 29 DefaultFunc: schema.EnvDefaultFunc("SDC_URL", "https://us-west-1.api.joyentcloud.com"), 30 }, 31 32 "key_material": &schema.Schema{ 33 Type: schema.TypeString, 34 Required: true, 35 DefaultFunc: schema.EnvDefaultFunc("SDC_KEY_MATERIAL", ""), 36 }, 37 38 "key_id": &schema.Schema{ 39 Type: schema.TypeString, 40 Required: true, 41 DefaultFunc: schema.EnvDefaultFunc("SDC_KEY_ID", ""), 42 }, 43 }, 44 45 ResourcesMap: map[string]*schema.Resource{ 46 "triton_firewall_rule": resourceFirewallRule(), 47 "triton_machine": resourceMachine(), 48 "triton_key": resourceKey(), 49 }, 50 ConfigureFunc: providerConfigure, 51 } 52 } 53 54 type SDCConfig struct { 55 Account string 56 KeyMaterial string 57 KeyID string 58 URL string 59 } 60 61 func (c SDCConfig) validate() error { 62 var err *multierror.Error 63 64 if c.URL == "" { 65 err = multierror.Append(err, fmt.Errorf("URL must be configured for the Triton provider")) 66 } 67 if c.KeyMaterial == "" { 68 err = multierror.Append(err, fmt.Errorf("Key Material must be configured for the Triton provider")) 69 } 70 if c.KeyID == "" { 71 err = multierror.Append(err, fmt.Errorf("Key ID must be configured for the Triton provider")) 72 } 73 if c.Account == "" { 74 err = multierror.Append(err, fmt.Errorf("Account must be configured for the Triton provider")) 75 } 76 77 return err.ErrorOrNil() 78 } 79 80 func (c SDCConfig) getSDCClient() (*cloudapi.Client, error) { 81 userauth, err := auth.NewAuth(c.Account, c.KeyMaterial, "rsa-sha256") 82 if err != nil { 83 return nil, err 84 } 85 86 creds := &auth.Credentials{ 87 UserAuthentication: userauth, 88 SdcKeyId: c.KeyID, 89 SdcEndpoint: auth.Endpoint{URL: c.URL}, 90 } 91 92 client := cloudapi.New(client.NewClient( 93 c.URL, 94 cloudapi.DefaultAPIVersion, 95 creds, 96 log.New(os.Stderr, "", log.LstdFlags), 97 )) 98 99 return client, nil 100 } 101 102 func providerConfigure(d *schema.ResourceData) (interface{}, error) { 103 config := SDCConfig{ 104 Account: d.Get("account").(string), 105 URL: d.Get("url").(string), 106 KeyMaterial: d.Get("key_material").(string), 107 KeyID: d.Get("key_id").(string), 108 } 109 110 if err := config.validate(); err != nil { 111 return nil, err 112 } 113 114 client, err := config.getSDCClient() 115 if err != nil { 116 return nil, err 117 } 118 119 return client, nil 120 }