Skip to content

Commit

Permalink
Merge pull request #45 from DrFaust92/ouath
Browse files Browse the repository at this point in the history
Oauth token support
  • Loading branch information
DrFaust92 authored Mar 14, 2022
2 parents 687a678 + 8ab13c4 commit bd8f1ea
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 23 deletions.
16 changes: 13 additions & 3 deletions bitbucket/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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.
Expand Down
42 changes: 33 additions & 9 deletions bitbucket/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
13 changes: 4 additions & 9 deletions bitbucket/provider_test.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 == "" {
Expand Down
7 changes: 5 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

0 comments on commit bd8f1ea

Please sign in to comment.