-
Notifications
You must be signed in to change notification settings - Fork 5
/
create_subaccount_destination.go
52 lines (45 loc) · 1.38 KB
/
create_subaccount_destination.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package clients
import (
"bytes"
models "cf-html5-apps-repo-cli-plugin/clients/models"
"cf-html5-apps-repo-cli-plugin/log"
"fmt"
"io/ioutil"
"net/http"
)
// CreateSubaccountDestination create destination service subaccount destination
func CreateSubaccountDestination(serviceURL string, accessToken string, destination models.DestinationConfiguration) error {
var err error
var request *http.Request
var response *http.Response
var destinationsURL string
var payload []byte
var body []byte
log.Tracef("Marshaling destination configuration: %+v\n", destination)
payload, err = destination.MarshalJSON()
if err != nil {
return err
}
log.Tracef("Destination configuration JSON: %s\n", payload)
destinationsURL = serviceURL + "/destination-configuration/v1/subaccountDestinations/"
log.Tracef("Making request to: %s\n", destinationsURL)
request, err = http.NewRequest("POST", destinationsURL, bytes.NewBuffer(payload))
if err != nil {
return err
}
request.Header.Set("Content-Type", "appication/json")
request.Header.Set("Authorization", "Bearer "+accessToken)
client := &http.Client{}
response, err = client.Do(request)
if err != nil {
return err
}
defer response.Body.Close()
if response.StatusCode > 201 {
body, err = ioutil.ReadAll(response.Body)
if err != nil {
return fmt.Errorf("Could not create destination: [%s] %+v", response.Status, body)
}
}
return nil
}