diff --git a/bitbucket/client.go b/bitbucket/client.go index 8cb0a47f..8dbd6fe5 100644 --- a/bitbucket/client.go +++ b/bitbucket/client.go @@ -32,8 +32,9 @@ const ( // Client is the base internal Client to talk to bitbuckets API. This should be a username and password // the password should be a app-password. type Client struct { - Username string - Password string + Username *string + Password *string + OAuthToken *string HTTPClient *http.Client } @@ -55,7 +56,16 @@ func (c *Client) Do(method, endpoint string, payload *bytes.Buffer, addJsonHeade return nil, err } - req.SetBasicAuth(c.Username, c.Password) + if c.Username != nil && c.Password != nil { + log.Printf("[DEBUG] Setting Basic Auth") + req.SetBasicAuth(*c.Username, *c.Password) + } + + if c.OAuthToken != nil { + log.Printf("[DEBUG] Setting Bearer Token") + var bearer = "Bearer " + *c.OAuthToken + req.Header.Add("Authorization", bearer) + } if payload != nil && addJsonHeader { // Can cause bad request when putting default reviews if set. diff --git a/bitbucket/provider.go b/bitbucket/provider.go index 4e21fc36..22c88bf2 100644 --- a/bitbucket/provider.go +++ b/bitbucket/provider.go @@ -7,19 +7,29 @@ import ( ) // Provider will create the necessary terraform provider to talk to the Bitbucket APIs you should -// specify a USERNAME and PASSWORD +// specify a USERNAME and PASSWORD or a OAUTH Token func Provider() *schema.Provider { return &schema.Provider{ Schema: map[string]*schema.Schema{ "username": { - Required: true, - Type: schema.TypeString, - DefaultFunc: schema.EnvDefaultFunc("BITBUCKET_USERNAME", nil), + Optional: true, + Type: schema.TypeString, + DefaultFunc: schema.EnvDefaultFunc("BITBUCKET_USERNAME", nil), + ConflictsWith: []string{"oauth_token"}, + RequiredWith: []string{"password"}, }, "password": { - Type: schema.TypeString, - Required: true, - DefaultFunc: schema.EnvDefaultFunc("BITBUCKET_PASSWORD", nil), + Type: schema.TypeString, + Optional: true, + DefaultFunc: schema.EnvDefaultFunc("BITBUCKET_PASSWORD", nil), + ConflictsWith: []string{"oauth_token"}, + RequiredWith: []string{"username"}, + }, + "oauth_token": { + Type: schema.TypeString, + Optional: true, + DefaultFunc: schema.EnvDefaultFunc("BITBUCKET_OAUTH_TOKEN", nil), + ConflictsWith: []string{"username", "password"}, }, }, ConfigureFunc: providerConfigure, @@ -54,11 +64,25 @@ func Provider() *schema.Provider { } func providerConfigure(d *schema.ResourceData) (interface{}, error) { + client := &Client{ - Username: d.Get("username").(string), - Password: d.Get("password").(string), HTTPClient: &http.Client{}, } + if v, ok := d.GetOk("username"); ok && v.(string) != "" { + user := v.(string) + client.Username = &user + } + + if v, ok := d.GetOk("password"); ok && v.(string) != "" { + pass := v.(string) + client.Password = &pass + } + + if v, ok := d.GetOk("oauth_token"); ok && v.(string) != "" { + token := v.(string) + client.OAuthToken = &token + } + return client, nil } diff --git a/bitbucket/provider_test.go b/bitbucket/provider_test.go index 52350e6c..aef9c35f 100644 --- a/bitbucket/provider_test.go +++ b/bitbucket/provider_test.go @@ -1,12 +1,11 @@ package bitbucket import ( - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "os" "testing" -) -const testRepo string = "test-repo" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) var testAccProviders map[string]*schema.Provider var testAccProvider *schema.Provider @@ -29,12 +28,8 @@ func TestProvider_impl(t *testing.T) { } func testAccPreCheck(t *testing.T) { - if v := os.Getenv("BITBUCKET_USERNAME"); v == "" { - t.Fatal("BITBUCKET_USERNAME must be set for acceptence tests") - } - - if v := os.Getenv("BITBUCKET_PASSWORD"); v == "" { - t.Fatal("BITBUCKET_PASSWORD must be set for acceptence tests") + if v := os.Getenv("BITBUCKET_OAUTH_TOKEN"); v == "" { + t.Fatal("BITBUCKET_OAUTH_TOKEN must be set for acceptence tests") } if v := os.Getenv("BITBUCKET_TEAM"); v == "" { diff --git a/docs/index.md b/docs/index.md index 8bd19dde..538e737f 100644 --- a/docs/index.md +++ b/docs/index.md @@ -41,8 +41,11 @@ resource "bitbucket_project" "project" { The following arguments are supported in the `provider` block: -* `username` - (Required) Your username used to connect to bitbucket. You can +* `username` - (Optional) Your username used to connect to bitbucket. You can also set this via the environment variable. `BITBUCKET_USERNAME` -* `password` - (Required) Your password used to connect to bitbucket. You can +* `password` - (Optional) Your password used to connect to bitbucket. You can also set this via the environment variable. `BITBUCKET_PASSWORD` + +* `oauth_token` - (Optional) Your password used to connect to bitbucket. You can +also set this via the environment variable. `BITBUCKET_OAUTH_TOKEN`