Install plugins with uri (#46)

bitbucket/resource_plugin: Install plugins with URI

Existing approach based on PostFileUpload() doesn't seem to work for Bitbucket server 6.10.
Replace it with InstallPluginWithUri().
This commit is contained in:
Stanislau Liamniou
2022-06-16 19:22:15 +02:00
committed by GitHub
parent 1323abe664
commit 5311e3ab9e
4 changed files with 121 additions and 75 deletions

View File

@@ -67,7 +67,7 @@ test-compile:
go test -c $(TEST) $(TESTARGS) go test -c $(TEST) $(TESTARGS)
ci-build-setup: 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 curl -L https://github.com/docker/compose/releases/download/1.25.4/docker-compose-`uname -s`-`uname -m` > docker-compose
chmod +x docker-compose chmod +x docker-compose
sudo mv docker-compose /usr/local/bin sudo mv docker-compose /usr/local/bin

View File

@@ -156,6 +156,62 @@ func (c *BitbucketClient) PostFileUpload(endpoint string, params map[string]stri
return resp, err 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) { func (c *BitbucketClient) Get(endpoint string) (*http.Response, error) {
return c.Do("GET", endpoint, nil, "application/json") return c.Do("GET", endpoint, nil, "application/json")
} }

View File

@@ -4,13 +4,13 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"io/ioutil" "io/ioutil"
"os"
"path/filepath" "path/filepath"
"regexp" "regexp"
"time" "time"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
) )
type Plugin struct { type Plugin struct {
@@ -263,24 +263,6 @@ func resourcePluginCreate(d *schema.ResourceData, m interface{}) error {
return err 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 // first get a token for interacting with the UPM
resp, err := provider.BitbucketClient.Get("/rest/plugins/1.0/?os_authType=basic") resp, err := provider.BitbucketClient.Get("/rest/plugins/1.0/?os_authType=basic")
if err != nil { if err != nil {
@@ -288,8 +270,10 @@ func resourcePluginCreate(d *schema.ResourceData, m interface{}) error {
} }
upmToken := resp.Header.Get("upm-token") upmToken := resp.Header.Get("upm-token")
// now we can use the token to upload the downloaded marketplace file to bitbucket pluginUri := marketplacePluginVersion.Embedded.Artifact.Links.Binary.Href
_, err = provider.BitbucketClient.PostFileUpload("/rest/plugins/1.0/?token="+upmToken, nil, "plugin", file.Name())
// 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 { if err != nil {
return err return err
} }

View File

@@ -1,53 +1,59 @@
package bitbucket package bitbucket
//func TestAccBitbucketPlugin_install(t *testing.T) { import (
// config := ` "testing"
// resource "bitbucketserver_plugin" "test" {
// key = "com.plugin.commitgraph.commitgraph" "github.com/hashicorp/terraform/helper/resource"
// version = "5.3.3" )
// license = "AAABCA0ODAoPeNpdj01PwkAURffzKyZxZ1IyUzARkllQ24gRaQMtGnaP8VEmtjPNfFT59yJVFyzfubkn796Ux0Bz6SmbUM5nbDzj97RISxozHpMUnbSq88poUaLztFEStUN6MJZ2TaiVpu/YY2M6tI6sQrtHmx8qd74EZ+TBIvyUU/AoYs7jiE0jzknWQxMuifA2IBlUbnQ7AulVjwN9AaU9atASs69O2dNFU4wXJLc1aOUGw9w34JwCTTZoe7RPqUgep2X0Vm0n0fNut4gSxl/Jcnj9nFb6Q5tP/Ueu3L+0PHW4ghZFmm2zZV5k6/95CbR7Y9bYGo/zGrV3Ir4jRbDyCA6vt34DO8p3SDAsAhQnJjLD5k9Fr3uaIzkXKf83o5vDdQIUe4XequNCC3D+9ht9ZYhNZFKmnhc=X02dh"
// } func TestAccBitbucketPlugin_install(t *testing.T) {
// ` config := `
// resource "bitbucketserver_plugin" "test" {
// resource.Test(t, resource.TestCase{ key = "com.plugin.commitgraph.commitgraph"
// PreCheck: func() { testAccPreCheck(t) }, version = "5.3.3"
// Providers: testAccProviders, license = "AAABCA0ODAoPeNpdj01PwkAURffzKyZxZ1IyUzARkllQ24gRaQMtGnaP8VEmtjPNfFT59yJVFyzfubkn796Ux0Bz6SmbUM5nbDzj97RISxozHpMUnbSq88poUaLztFEStUN6MJZ2TaiVpu/YY2M6tI6sQrtHmx8qd74EZ+TBIvyUU/AoYs7jiE0jzknWQxMuifA2IBlUbnQ7AulVjwN9AaU9atASs69O2dNFU4wXJLc1aOUGw9w34JwCTTZoe7RPqUgep2X0Vm0n0fNut4gSxl/Jcnj9nFb6Q5tP/Ueu3L+0PHW4ghZFmm2zZV5k6/95CbR7Y9bYGo/zGrV3Ir4jRbDyCA6vt34DO8p3SDAsAhQnJjLD5k9Fr3uaIzkXKf83o5vDdQIUe4XequNCC3D+9ht9ZYhNZFKmnhc=X02dh"
// Steps: []resource.TestStep{ }
// { `
// Config: config,
// Check: resource.ComposeTestCheckFunc( resource.Test(t, resource.TestCase{
// resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "key", "com.plugin.commitgraph.commitgraph"), PreCheck: func() { testAccPreCheck(t) },
// resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "enabled", "true"), Providers: testAccProviders,
// resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "version", "5.3.3"), Steps: []resource.TestStep{
// 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"), Config: config,
// resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "name", "Charts and Graphs for Bitbucket Server"), Check: resource.ComposeTestCheckFunc(
// 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", "key", "com.plugin.commitgraph.commitgraph"),
// resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "user_installed", "true"), resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "enabled", "true"),
// resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "optional", "true"), resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "version", "5.3.3"),
// resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "vendor.name", "Mohami"), resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "license", "AAABCA0ODAoPeNpdj01PwkAURffzKyZxZ1IyUzARkllQ24gRaQMtGnaP8VEmtjPNfFT59yJVFyzfubkn796Ux0Bz6SmbUM5nbDzj97RISxozHpMUnbSq88poUaLztFEStUN6MJZ2TaiVpu/YY2M6tI6sQrtHmx8qd74EZ+TBIvyUU/AoYs7jiE0jzknWQxMuifA2IBlUbnQ7AulVjwN9AaU9atASs69O2dNFU4wXJLc1aOUGw9w34JwCTTZoe7RPqUgep2X0Vm0n0fNut4gSxl/Jcnj9nFb6Q5tP/Ueu3L+0PHW4ghZFmm2zZV5k6/95CbR7Y9bYGo/zGrV3Ir4jRbDyCA6vt34DO8p3SDAsAhQnJjLD5k9Fr3uaIzkXKf83o5vDdQIUe4XequNCC3D+9ht9ZYhNZFKmnhc=X02dh"),
// resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "vendor.link", "https://mohami.io"), resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "enabled_by_default", "true"),
// resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "vendor.marketplace_link", "https://mohami.io"), resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "name", "Charts and Graphs for Bitbucket Server"),
// resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.valid", "true"), 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", "applied_license.0.evaluation", "true"), resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "user_installed", "true"),
// resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.nearly_expired", "true"), resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "optional", "true"),
// resource.TestCheckResourceAttrSet("bitbucketserver_plugin.test", "applied_license.0.maintenance_expiry_date"), resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "vendor.name", "Mohami"),
// resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.maintenance_expired", "false"), resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "vendor.link", "https://mohami.io"),
// resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.license_type", "DEVELOPER"), resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "vendor.marketplace_link", "https://mohami.io"),
// resource.TestCheckResourceAttrSet("bitbucketserver_plugin.test", "applied_license.0.expiry_date"), resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.valid", "true"),
// 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.evaluation", "true"),
// resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.renewable", "false"), resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.nearly_expired", "true"),
// resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.organization_name", "Atlassian"), resource.TestCheckResourceAttrSet("bitbucketserver_plugin.test", "applied_license.0.maintenance_expiry_date"),
// resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.enterprise", "false"), resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.maintenance_expired", "false"),
// resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.data_center", "false"), resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.license_type", "DEVELOPER"),
// resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.subscription", "false"), resource.TestCheckResourceAttrSet("bitbucketserver_plugin.test", "applied_license.0.expiry_date"),
// resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.active", "true"), 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.auto_renewal", "false"), resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.renewable", "false"),
// resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.upgradable", "false"), resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.organization_name", "Atlassian"),
// resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.crossgradeable", "false"), resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.enterprise", "false"),
// resource.TestCheckResourceAttr("bitbucketserver_plugin.test", "applied_license.0.purchase_past_server_cutoff_date", "true"), 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"),
),
},
},
})
}