-
Notifications
You must be signed in to change notification settings - Fork 6
/
create_service_instance.go
69 lines (62 loc) · 2.11 KB
/
create_service_instance.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package clients
import (
models "cf-html5-apps-repo-cli-plugin/clients/models"
"cf-html5-apps-repo-cli-plugin/log"
"encoding/json"
"errors"
"strconv"
"strings"
"time"
"github.com/cloudfoundry/cli/plugin"
)
// CreateServiceInstance create Cloud Foundry service instance
func CreateServiceInstance(cliConnection plugin.CliConnection, spaceGUID string, servicePlan models.CFServicePlan, parameters interface{}, name string) (*models.CFServiceInstance, error) {
var serviceInstance *models.CFServiceInstance
var responseObject models.CFResource
var errorResponseObject models.CFErrorResponse
var responseStrings []string
var responseBytes []byte
var err error
var url string
var serviceParameters string
var body string
t := strconv.FormatInt(time.Now().Unix(), 10)
url = "/v2/service_instances"
if parameters != nil {
parametersBytes, err := json.Marshal(parameters)
if err != nil {
return nil, err
}
serviceParameters = "\"parameters\":" + string(parametersBytes) + ","
} else {
serviceParameters = ""
}
if name == "" {
name = servicePlan.Name + "-" + t
} else if len(name) > 1 && name[len(name)-1:] == "-" {
name = name + servicePlan.Name + "-" + t
}
body = "'{" + serviceParameters + "\"space_guid\":\"" + spaceGUID + "\",\"name\":\"" + name + "\",\"service_plan_guid\":\"" + servicePlan.GUID + "\"}'"
log.Tracef("Making request to: %s\n", url)
responseStrings, err = cliConnection.CliCommandWithoutTerminalOutput("curl", url, "-X", "POST", "-d", body)
if err != nil {
return nil, err
}
responseBytes = []byte(strings.Join(responseStrings, ""))
err = json.Unmarshal(responseBytes, &responseObject)
if err != nil {
return nil, err
}
if responseObject.Entity == nil {
err = json.Unmarshal(responseBytes, &errorResponseObject)
if err != nil {
return nil, err
}
if errorResponseObject.Description != nil {
return nil, errors.New("\n" + *errorResponseObject.Description)
}
return nil, errors.New(strings.Join(responseStrings, "\n"))
}
serviceInstance = &models.CFServiceInstance{GUID: responseObject.Metadata.GUID, Name: *responseObject.Entity.Name}
return serviceInstance, nil
}