diff --git a/Makefile b/Makefile index abca9db..cedb640 100644 --- a/Makefile +++ b/Makefile @@ -67,7 +67,7 @@ test-compile: go test -c $(TEST) $(TESTARGS) ci-build-setup: - sudo rm /usr/local/bin/docker-compose + sudo rm -f /usr/local/bin/docker-compose curl -L https://github.com/docker/compose/releases/download/1.25.4/docker-compose-`uname -s`-`uname -m` > docker-compose chmod +x docker-compose sudo mv docker-compose /usr/local/bin diff --git a/bitbucket/client.go b/bitbucket/client.go index e4a7050..e37bda0 100644 --- a/bitbucket/client.go +++ b/bitbucket/client.go @@ -156,6 +156,62 @@ func (c *BitbucketClient) PostFileUpload(endpoint string, params map[string]stri return resp, err } +type PluginInstallPayload struct { + PluginURI string `json:"pluginUri"` + PluginName string `json:"pluginName"` +} + +func (c *BitbucketClient) InstallPluginWithUri(endpoint string, uri string, pluginName string) (*http.Response, error) { + // The method implements this functionality + // https://developer.atlassian.com/platform/marketplace/registering-apps/#installing-an-app-using-the-rest-api + absoluteendpoint := c.Server + endpoint + log.Printf("[DEBUG] Sending request to POST %s", absoluteendpoint) + + installPayload := PluginInstallPayload{ + PluginURI: uri, + PluginName: pluginName, + } + + bytedata, err := json.Marshal(installPayload) + if err != nil { + return nil, err + } + + req, err := http.NewRequest("POST", absoluteendpoint, bytes.NewBuffer(bytedata)) + if err != nil { + return nil, err + } + + req.Header.Set("Content-Type", "application/vnd.atl.plugins.install.uri+json") + + req.SetBasicAuth(c.Username, c.Password) + req.Header.Add("X-Atlassian-Token", "no-check") + req.Header.Add("Accept", "application/json") + req.Close = true + + resp, err := c.HTTPClient.Do(req) + log.Printf("[DEBUG] Resp: %v Err: %v", resp, err) + if resp != nil && (resp.StatusCode >= 400 || resp.StatusCode < 200) { + apiError := Error{ + StatusCode: resp.StatusCode, + Endpoint: endpoint, + } + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + + log.Printf("[DEBUG] Resp Body: %s", string(body)) + + _ = json.Unmarshal(body, &apiError) + return resp, error(apiError) + + } + + return resp, err +} + func (c *BitbucketClient) Get(endpoint string) (*http.Response, error) { return c.Do("GET", endpoint, nil, "application/json") } diff --git a/bitbucket/resource_plugin.go b/bitbucket/resource_plugin.go index 8b65ddd..e3d6012 100644 --- a/bitbucket/resource_plugin.go +++ b/bitbucket/resource_plugin.go @@ -4,13 +4,13 @@ import ( "bytes" "encoding/json" "fmt" - "github.com/hashicorp/terraform/helper/resource" - "github.com/hashicorp/terraform/helper/schema" "io/ioutil" - "os" "path/filepath" "regexp" "time" + + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/helper/schema" ) type Plugin struct { @@ -263,24 +263,6 @@ func resourcePluginCreate(d *schema.ResourceData, m interface{}) error { return err } - file, err := ioutil.TempFile(os.TempDir(), "*"+marketplacePluginVersion.Filename()) - if err != nil { - return err - } - - defer os.Remove(file.Name()) - - // download the plugin artifact for uploading to the API - err = provider.MarketplaceClient.DownloadArtifact(marketplacePluginVersion.Embedded.Artifact.Links.Binary.Href, file) - if err != nil { - return err - } - - err = file.Close() - if err != nil { - return err - } - // first get a token for interacting with the UPM resp, err := provider.BitbucketClient.Get("/rest/plugins/1.0/?os_authType=basic") if err != nil { @@ -288,8 +270,10 @@ func resourcePluginCreate(d *schema.ResourceData, m interface{}) error { } upmToken := resp.Header.Get("upm-token") - // now we can use the token to upload the downloaded marketplace file to bitbucket - _, err = provider.BitbucketClient.PostFileUpload("/rest/plugins/1.0/?token="+upmToken, nil, "plugin", file.Name()) + pluginUri := marketplacePluginVersion.Embedded.Artifact.Links.Binary.Href + + // now we can use the token to install plugin to Bitbucket + _, err = provider.BitbucketClient.InstallPluginWithUri("/rest/plugins/1.0/?token="+upmToken, pluginUri, d.Get("key").(string)) if err != nil { return err } diff --git a/bitbucket/resource_plugin_test.go b/bitbucket/resource_plugin_test.go index fd53474..17c9512 100644 --- a/bitbucket/resource_plugin_test.go +++ b/bitbucket/resource_plugin_test.go @@ -1,53 +1,59 @@ package bitbucket -//func TestAccBitbucketPlugin_install(t *testing.T) { -// config := ` -// resource "bitbucketserver_plugin" "test" { -// key = "com.plugin.commitgraph.commitgraph" -// version = "5.3.3" -// license = "AAABCA0ODAoPeNpdj01PwkAURffzKyZxZ1IyUzARkllQ24gRaQMtGnaP8VEmtjPNfFT59yJVFyzfubkn796Ux0Bz6SmbUM5nbDzj97RISxozHpMUnbSq88poUaLztFEStUN6MJZ2TaiVpu/YY2M6tI6sQrtHmx8qd74EZ+TBIvyUU/AoYs7jiE0jzknWQxMuifA2IBlUbnQ7AulVjwN9AaU9atASs69O2dNFU4wXJLc1aOUGw9w34JwCTTZoe7RPqUgep2X0Vm0n0fNut4gSxl/Jcnj9nFb6Q5tP/Ueu3L+0PHW4ghZFmm2zZV5k6/95CbR7Y9bYGo/zGrV3Ir4jRbDyCA6vt34DO8p3SDAsAhQnJjLD5k9Fr3uaIzkXKf83o5vDdQIUe4XequNCC3D+9ht9ZYhNZFKmnhc=X02dh" -// } -// ` -// -// resource.Test(t, resource.TestCase{ -// PreCheck: func() { testAccPreCheck(t) }, -// Providers: testAccProviders, -// Steps: []resource.TestStep{ -// { -// Config: config, -// Check: resource.ComposeTestCheckFunc( -// resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "key", "com.plugin.commitgraph.commitgraph"), -// resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "enabled", "true"), -// resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "version", "5.3.3"), -// resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "license", "AAABCA0ODAoPeNpdj01PwkAURffzKyZxZ1IyUzARkllQ24gRaQMtGnaP8VEmtjPNfFT59yJVFyzfubkn796Ux0Bz6SmbUM5nbDzj97RISxozHpMUnbSq88poUaLztFEStUN6MJZ2TaiVpu/YY2M6tI6sQrtHmx8qd74EZ+TBIvyUU/AoYs7jiE0jzknWQxMuifA2IBlUbnQ7AulVjwN9AaU9atASs69O2dNFU4wXJLc1aOUGw9w34JwCTTZoe7RPqUgep2X0Vm0n0fNut4gSxl/Jcnj9nFb6Q5tP/Ueu3L+0PHW4ghZFmm2zZV5k6/95CbR7Y9bYGo/zGrV3Ir4jRbDyCA6vt34DO8p3SDAsAhQnJjLD5k9Fr3uaIzkXKf83o5vDdQIUe4XequNCC3D+9ht9ZYhNZFKmnhc=X02dh"), -// resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "enabled_by_default", "true"), -// resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "name", "Charts and Graphs for Bitbucket Server"), -// resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "description", "Gain insight into Bitbucket with charts and graphs that help you visualize user contributions, commits, and team activity."), -// resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "user_installed", "true"), -// resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "optional", "true"), -// resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "vendor.name", "Mohami"), -// resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "vendor.link", "https://mohami.io"), -// resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "vendor.marketplace_link", "https://mohami.io"), -// resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.valid", "true"), -// resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.evaluation", "true"), -// resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.nearly_expired", "true"), -// resource.TestCheckResourceAttrSet("bitbucketserver_plugin.test", "applied_license.0.maintenance_expiry_date"), -// resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.maintenance_expired", "false"), -// resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.license_type", "DEVELOPER"), -// resource.TestCheckResourceAttrSet("bitbucketserver_plugin.test", "applied_license.0.expiry_date"), -// resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.raw_license", "AAABCA0ODAoPeNpdj01PwkAURffzKyZxZ1IyUzARkllQ24gRaQMtGnaP8VEmtjPNfFT59yJVFyzfubkn796Ux0Bz6SmbUM5nbDzj97RISxozHpMUnbSq88poUaLztFEStUN6MJZ2TaiVpu/YY2M6tI6sQrtHmx8qd74EZ+TBIvyUU/AoYs7jiE0jzknWQxMuifA2IBlUbnQ7AulVjwN9AaU9atASs69O2dNFU4wXJLc1aOUGw9w34JwCTTZoe7RPqUgep2X0Vm0n0fNut4gSxl/Jcnj9nFb6Q5tP/Ueu3L+0PHW4ghZFmm2zZV5k6/95CbR7Y9bYGo/zGrV3Ir4jRbDyCA6vt34DO8p3SDAsAhQnJjLD5k9Fr3uaIzkXKf83o5vDdQIUe4XequNCC3D+9ht9ZYhNZFKmnhc=X02dh"), -// resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.renewable", "false"), -// resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.organization_name", "Atlassian"), -// resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.enterprise", "false"), -// resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.data_center", "false"), -// resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.subscription", "false"), -// resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.active", "true"), -// resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.auto_renewal", "false"), -// resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.upgradable", "false"), -// resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.crossgradeable", "false"), -// resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.purchase_past_server_cutoff_date", "true"), -// ), -// }, -// }, -// }) -//} +import ( + "testing" + + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccBitbucketPlugin_install(t *testing.T) { + config := ` + resource "bitbucketserver_plugin" "test" { + key = "com.plugin.commitgraph.commitgraph" + version = "5.3.3" + license = "AAABCA0ODAoPeNpdj01PwkAURffzKyZxZ1IyUzARkllQ24gRaQMtGnaP8VEmtjPNfFT59yJVFyzfubkn796Ux0Bz6SmbUM5nbDzj97RISxozHpMUnbSq88poUaLztFEStUN6MJZ2TaiVpu/YY2M6tI6sQrtHmx8qd74EZ+TBIvyUU/AoYs7jiE0jzknWQxMuifA2IBlUbnQ7AulVjwN9AaU9atASs69O2dNFU4wXJLc1aOUGw9w34JwCTTZoe7RPqUgep2X0Vm0n0fNut4gSxl/Jcnj9nFb6Q5tP/Ueu3L+0PHW4ghZFmm2zZV5k6/95CbR7Y9bYGo/zGrV3Ir4jRbDyCA6vt34DO8p3SDAsAhQnJjLD5k9Fr3uaIzkXKf83o5vDdQIUe4XequNCC3D+9ht9ZYhNZFKmnhc=X02dh" + } + ` + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "key", "com.plugin.commitgraph.commitgraph"), + resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "enabled", "true"), + resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "version", "5.3.3"), + resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "license", "AAABCA0ODAoPeNpdj01PwkAURffzKyZxZ1IyUzARkllQ24gRaQMtGnaP8VEmtjPNfFT59yJVFyzfubkn796Ux0Bz6SmbUM5nbDzj97RISxozHpMUnbSq88poUaLztFEStUN6MJZ2TaiVpu/YY2M6tI6sQrtHmx8qd74EZ+TBIvyUU/AoYs7jiE0jzknWQxMuifA2IBlUbnQ7AulVjwN9AaU9atASs69O2dNFU4wXJLc1aOUGw9w34JwCTTZoe7RPqUgep2X0Vm0n0fNut4gSxl/Jcnj9nFb6Q5tP/Ueu3L+0PHW4ghZFmm2zZV5k6/95CbR7Y9bYGo/zGrV3Ir4jRbDyCA6vt34DO8p3SDAsAhQnJjLD5k9Fr3uaIzkXKf83o5vDdQIUe4XequNCC3D+9ht9ZYhNZFKmnhc=X02dh"), + resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "enabled_by_default", "true"), + resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "name", "Charts and Graphs for Bitbucket Server"), + resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "description", "Gain insight into Bitbucket with charts and graphs that help you visualize user contributions, commits, and team activity."), + resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "user_installed", "true"), + resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "optional", "true"), + resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "vendor.name", "Mohami"), + resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "vendor.link", "https://mohami.io"), + resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "vendor.marketplace_link", "https://mohami.io"), + resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.valid", "true"), + resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.evaluation", "true"), + resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.nearly_expired", "true"), + resource.TestCheckResourceAttrSet("bitbucketserver_plugin.test", "applied_license.0.maintenance_expiry_date"), + resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.maintenance_expired", "false"), + resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.license_type", "DEVELOPER"), + resource.TestCheckResourceAttrSet("bitbucketserver_plugin.test", "applied_license.0.expiry_date"), + resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.raw_license", "AAABCA0ODAoPeNpdj01PwkAURffzKyZxZ1IyUzARkllQ24gRaQMtGnaP8VEmtjPNfFT59yJVFyzfubkn796Ux0Bz6SmbUM5nbDzj97RISxozHpMUnbSq88poUaLztFEStUN6MJZ2TaiVpu/YY2M6tI6sQrtHmx8qd74EZ+TBIvyUU/AoYs7jiE0jzknWQxMuifA2IBlUbnQ7AulVjwN9AaU9atASs69O2dNFU4wXJLc1aOUGw9w34JwCTTZoe7RPqUgep2X0Vm0n0fNut4gSxl/Jcnj9nFb6Q5tP/Ueu3L+0PHW4ghZFmm2zZV5k6/95CbR7Y9bYGo/zGrV3Ir4jRbDyCA6vt34DO8p3SDAsAhQnJjLD5k9Fr3uaIzkXKf83o5vDdQIUe4XequNCC3D+9ht9ZYhNZFKmnhc=X02dh"), + resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.renewable", "false"), + resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.organization_name", "Atlassian"), + resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.enterprise", "false"), + resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.data_center", "false"), + resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.subscription", "false"), + resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.active", "true"), + resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.auto_renewal", "false"), + resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.upgradable", "false"), + resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.crossgradeable", "false"), + resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.purchase_past_server_cutoff_date", "true"), + ), + }, + }, + }) +}