-
Notifications
You must be signed in to change notification settings - Fork 55
/
service_instance.go
95 lines (86 loc) · 3.32 KB
/
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package presenter
import (
"net/url"
"code.cloudfoundry.org/korifi/api/repositories"
)
const (
serviceInstancesBase = "/v3/service_instances"
serviceRouteBindingsBase = "/v3/service_route_bindings"
)
type ServiceInstanceResponse struct {
Name string `json:"name"`
GUID string `json:"guid"`
Type string `json:"type"`
Tags []string `json:"tags"`
LastOperation lastOperation `json:"last_operation"`
RouteServiceURL *string `json:"route_service_url"`
SyslogDrainURL *string `json:"syslog_drain_url"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
Relationships Relationships `json:"relationships"`
Metadata Metadata `json:"metadata"`
Links ServiceInstanceLinks `json:"links"`
}
type lastOperation struct {
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
Description string `json:"description"`
State string `json:"state"`
Type string `json:"type"`
}
type ServiceInstanceLinks struct {
Self Link `json:"self"`
Space Link `json:"space"`
Credentials Link `json:"credentials"`
ServiceCredentialBindings Link `json:"service_credential_bindings"`
ServiceRouteBindings Link `json:"service_route_bindings"`
}
func ForServiceInstance(serviceInstanceRecord repositories.ServiceInstanceRecord, baseURL url.URL) ServiceInstanceResponse {
lastOperationType := "update"
if serviceInstanceRecord.UpdatedAt == nil || serviceInstanceRecord.CreatedAt == *serviceInstanceRecord.UpdatedAt {
lastOperationType = "create"
}
return ServiceInstanceResponse{
Name: serviceInstanceRecord.Name,
GUID: serviceInstanceRecord.GUID,
Type: serviceInstanceRecord.Type,
Tags: emptySliceIfNil(serviceInstanceRecord.Tags),
LastOperation: lastOperation{
CreatedAt: formatTimestamp(&serviceInstanceRecord.CreatedAt),
UpdatedAt: formatTimestamp(serviceInstanceRecord.UpdatedAt),
Description: "Operation succeeded",
State: "succeeded",
Type: lastOperationType,
},
CreatedAt: formatTimestamp(&serviceInstanceRecord.CreatedAt),
UpdatedAt: formatTimestamp(serviceInstanceRecord.UpdatedAt),
Relationships: Relationships{
"space": Relationship{
Data: &RelationshipData{
GUID: serviceInstanceRecord.SpaceGUID,
},
},
},
Metadata: Metadata{
Labels: emptyMapIfNil(serviceInstanceRecord.Labels),
Annotations: emptyMapIfNil(serviceInstanceRecord.Annotations),
},
Links: ServiceInstanceLinks{
Self: Link{
HRef: buildURL(baseURL).appendPath(serviceInstancesBase, serviceInstanceRecord.GUID).build(),
},
Space: Link{
HRef: buildURL(baseURL).appendPath(spacesBase, serviceInstanceRecord.SpaceGUID).build(),
},
Credentials: Link{
HRef: buildURL(baseURL).appendPath(serviceInstancesBase, serviceInstanceRecord.GUID, "credentials").build(),
},
ServiceCredentialBindings: Link{
HRef: buildURL(baseURL).appendPath(serviceCredentialBindingsBase).setQuery("service_instance_guids=" + serviceInstanceRecord.GUID).build(),
},
ServiceRouteBindings: Link{
HRef: buildURL(baseURL).appendPath(serviceRouteBindingsBase).setQuery("service_instance_guids=" + serviceInstanceRecord.GUID).build(),
},
},
}
}