mirror of
https://github.com/ysoftdevs/terraform-provider-bitbucketserver.git
synced 2026-04-20 07:41:19 +02:00
Added resource.bitbucketserver_plugin
This commit is contained in:
@@ -7,7 +7,10 @@ import (
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// Error represents a error from the bitbucket api.
|
||||
@@ -40,7 +43,7 @@ type BitbucketClient struct {
|
||||
HTTPClient *http.Client
|
||||
}
|
||||
|
||||
func (c *BitbucketClient) Do(method, endpoint string, payload *bytes.Buffer) (*http.Response, error) {
|
||||
func (c *BitbucketClient) Do(method, endpoint string, payload *bytes.Buffer, contentType string) (*http.Response, error) {
|
||||
|
||||
absoluteendpoint := c.Server + endpoint
|
||||
log.Printf("[DEBUG] Sending request to %s %s", method, absoluteendpoint)
|
||||
@@ -61,8 +64,11 @@ func (c *BitbucketClient) Do(method, endpoint string, payload *bytes.Buffer) (*h
|
||||
req.Header.Add("X-Atlassian-Token", "no-check")
|
||||
|
||||
if payload != nil {
|
||||
// Can cause bad request when putting default reviews if set.
|
||||
req.Header.Add("Content-Type", "application/json")
|
||||
if contentType != "" {
|
||||
req.Header.Add("Content-Type", contentType)
|
||||
} else {
|
||||
req.Header.Add("Content-Type", "application/json")
|
||||
}
|
||||
}
|
||||
|
||||
req.Close = true
|
||||
@@ -89,22 +95,83 @@ func (c *BitbucketClient) Do(method, endpoint string, payload *bytes.Buffer) (*h
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// Creates a new file upload http request with optional extra params
|
||||
func (c *BitbucketClient) PostFileUpload(endpoint string, params map[string]string, paramName, path string) (*http.Response, error) {
|
||||
absoluteendpoint := c.Server + endpoint
|
||||
log.Printf("[DEBUG] Sending request to POST %s", absoluteendpoint)
|
||||
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
body := &bytes.Buffer{}
|
||||
writer := multipart.NewWriter(body)
|
||||
part, err := writer.CreateFormFile(paramName, filepath.Base(path))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, err = io.Copy(part, file)
|
||||
|
||||
for key, val := range params {
|
||||
_ = writer.WriteField(key, val)
|
||||
}
|
||||
err = writer.Close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", absoluteendpoint, body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", writer.FormDataContentType())
|
||||
|
||||
req.SetBasicAuth(c.Username, c.Password)
|
||||
req.Header.Add("X-Atlassian-Token", "no-check")
|
||||
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)
|
||||
return c.Do("GET", endpoint, nil, "application/json")
|
||||
}
|
||||
|
||||
func (c *BitbucketClient) Post(endpoint string, jsonpayload *bytes.Buffer) (*http.Response, error) {
|
||||
return c.Do("POST", endpoint, jsonpayload)
|
||||
return c.Do("POST", endpoint, jsonpayload, "application/json")
|
||||
}
|
||||
|
||||
func (c *BitbucketClient) Put(endpoint string, jsonpayload *bytes.Buffer) (*http.Response, error) {
|
||||
return c.Do("PUT", endpoint, jsonpayload)
|
||||
return c.Do("PUT", endpoint, jsonpayload, "application/json")
|
||||
}
|
||||
|
||||
func (c *BitbucketClient) PutOnly(endpoint string) (*http.Response, error) {
|
||||
return c.Do("PUT", endpoint, nil)
|
||||
return c.Do("PUT", endpoint, nil, "application/json")
|
||||
}
|
||||
|
||||
func (c *BitbucketClient) Delete(endpoint string) (*http.Response, error) {
|
||||
return c.Do("DELETE", endpoint, nil)
|
||||
return c.Do("DELETE", endpoint, nil, "application/json")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user