Skip to content

Commit 5ec6e12

Browse files
authored
Merge pull request #502 from zavitaev-a/add-stack-state
Add state and state_reason to Stack object as described in https://v3-apidocs.cloudfoundry.org/version/3.212.0/index.html#stacks
2 parents a29847b + 1865de7 commit 5ec6e12

4 files changed

Lines changed: 94 additions & 1 deletion

File tree

client/stack_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ func TestStacks(t *testing.T) {
1313
g := testutil.NewObjectJSONGenerator()
1414
stack := g.Stack().JSON
1515
stack2 := g.Stack().JSON
16+
stackWithState := g.StackWithState("ACTIVE", "Stack is fully available").JSON
17+
stackWithState2 := g.StackWithState("DEPRECATED", "Stack deprecated and will be removed").JSON
1618
app := g.Application().JSON
1719
app2 := g.Application().JSON
1820

@@ -36,6 +38,29 @@ func TestStacks(t *testing.T) {
3638
return c.Stacks.Create(context.Background(), r)
3739
},
3840
},
41+
{
42+
Description: "Create stack with state",
43+
Route: testutil.MockRoute{
44+
Method: "POST",
45+
Endpoint: "/v3/stacks",
46+
Output: g.Single(stackWithState),
47+
Status: http.StatusCreated,
48+
PostForm: `{ "name": "my-stack", "description": "Here is my stack!", "state": "ACTIVE", "state_reason": "Stack is fully available" }`,
49+
},
50+
Expected: stackWithState,
51+
Action: func(c *Client, t *testing.T) (any, error) {
52+
stackDescription := "Here is my stack!"
53+
stackState := "ACTIVE"
54+
stackStateReason := "Stack is fully available"
55+
r := &resource.StackCreate{
56+
Name: "my-stack",
57+
Description: &stackDescription,
58+
State: &stackState,
59+
StateReason: &stackStateReason,
60+
}
61+
return c.Stacks.Create(context.Background(), r)
62+
},
63+
},
3964
{
4065
Description: "Delete stack",
4166
Route: testutil.MockRoute{
@@ -59,6 +84,18 @@ func TestStacks(t *testing.T) {
5984
return c.Stacks.Get(context.Background(), "88db2b75-671f-4e4b-a19a-7db992366595")
6085
},
6186
},
87+
{
88+
Description: "Get stack with state",
89+
Route: testutil.MockRoute{
90+
Method: "GET",
91+
Endpoint: "/v3/stacks/88db2b75-671f-4e4b-a19a-7db992366595",
92+
Output: g.Single(stackWithState),
93+
Status: http.StatusOK},
94+
Expected: stackWithState,
95+
Action: func(c *Client, t *testing.T) (any, error) {
96+
return c.Stacks.Get(context.Background(), "88db2b75-671f-4e4b-a19a-7db992366595")
97+
},
98+
},
6299
{
63100
Description: "List all stacks",
64101
Route: testutil.MockRoute{
@@ -71,6 +108,18 @@ func TestStacks(t *testing.T) {
71108
return c.Stacks.ListAll(context.Background(), nil)
72109
},
73110
},
111+
{
112+
Description: "List all stacks with state",
113+
Route: testutil.MockRoute{
114+
Method: "GET",
115+
Endpoint: "/v3/stacks",
116+
Output: g.Paged([]string{stackWithState}, []string{stackWithState2}),
117+
Status: http.StatusOK},
118+
Expected: g.Array(stackWithState, stackWithState2),
119+
Action: func(c *Client, t *testing.T) (any, error) {
120+
return c.Stacks.ListAll(context.Background(), nil)
121+
},
122+
},
74123
{
75124
Description: "List all apps for given stack",
76125
Route: testutil.MockRoute{
@@ -102,6 +151,26 @@ func TestStacks(t *testing.T) {
102151
return c.Stacks.Update(context.Background(), "88db2b75-671f-4e4b-a19a-7db992366595", r)
103152
},
104153
},
154+
{
155+
Description: "Update stack state",
156+
Route: testutil.MockRoute{
157+
Method: "PATCH",
158+
Endpoint: "/v3/stacks/88db2b75-671f-4e4b-a19a-7db992366595",
159+
Output: g.Single(stackWithState2),
160+
Status: http.StatusOK,
161+
PostForm: `{ "state": "DEPRECATED", "state_reason": "Stack deprecated and will be removed" }`,
162+
},
163+
Expected: stackWithState2,
164+
Action: func(c *Client, t *testing.T) (any, error) {
165+
state := "DEPRECATED"
166+
stateReason := "Stack deprecated and will be removed"
167+
r := &resource.StackUpdate{
168+
State: &state,
169+
StateReason: &stateReason,
170+
}
171+
return c.Stacks.Update(context.Background(), "88db2b75-671f-4e4b-a19a-7db992366595", r)
172+
},
173+
},
105174
}
106175
ExecuteTests(tests, t)
107176
}

resource/stack.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ package resource
66
type Stack struct {
77
Name string `json:"name"`
88
Description *string `json:"description"`
9+
State *string `json:"state,omitempty"`
10+
StateReason *string `json:"state_reason,omitempty"`
911
RunRootfsImage string `json:"run_rootfs_image"`
1012
BuildRootfsImage string `json:"build_rootfs_image"`
1113
Default bool `json:"default"`
@@ -16,11 +18,15 @@ type Stack struct {
1618
type StackCreate struct {
1719
Name string `json:"name"`
1820
Description *string `json:"description,omitempty"`
21+
State *string `json:"state,omitempty"`
22+
StateReason *string `json:"state_reason,omitempty"`
1923
Metadata *Metadata `json:"metadata,omitempty"`
2024
}
2125

2226
type StackUpdate struct {
23-
Metadata *Metadata `json:"metadata,omitempty"`
27+
Metadata *Metadata `json:"metadata,omitempty"`
28+
State *string `json:"state,omitempty"`
29+
StateReason *string `json:"state_reason,omitempty"`
2430
}
2531

2632
type StackList struct {

testutil/object_generator.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,18 @@ func (o ObjectJSONGenerator) Stack() *JSONResource {
455455
return o.renderTemplate(r, "stack.json")
456456
}
457457

458+
func (o ObjectJSONGenerator) StackWithState(state, stateReason string) *JSONResource {
459+
r := &JSONResource{
460+
GUID: RandomGUID(),
461+
Name: RandomName(),
462+
Params: map[string]string{
463+
"state": state,
464+
"state_reason": stateReason,
465+
},
466+
}
467+
return o.renderTemplate(r, "stack.json")
468+
}
469+
458470
func (o ObjectJSONGenerator) Task() *JSONResource {
459471
r := &JSONResource{
460472
GUID: RandomGUID(),

testutil/template/stack.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
"updated_at": "2018-11-09T22:43:28Z",
55
"name": "{{.Name}}",
66
"description": "Here is my stack!",
7+
{{- if index .Params "state"}}
8+
"state": "{{index .Params "state"}}",
9+
{{- end}}
10+
{{- if index .Params "state_reason"}}
11+
"state_reason": "{{index .Params "state_reason"}}",
12+
{{- end}}
713
"build_rootfs_image": "{{.Name}}",
814
"run_rootfs_image": "{{.Name}}",
915
"default": false,

0 commit comments

Comments
 (0)