forked from eclipse-iofog/iofog-go-sdk
/
flows.go
99 lines (89 loc) · 2.9 KB
/
flows.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
96
97
98
99
/*
* *******************************************************************************
* * Copyright (c) 2019 Edgeworx, Inc.
* *
* * This program and the accompanying materials are made available under the
* * terms of the Eclipse Public License v. 2.0 which is available at
* * http://www.eclipse.org/legal/epl-2.0
* *
* * SPDX-License-Identifier: EPL-2.0
* *******************************************************************************
*
*/
package client
import (
"encoding/json"
"fmt"
)
// GetFlowByID retrieve flow information using the Controller REST API
func (clt *Client) GetFlowByID(id int) (flow *FlowInfo, err error) {
body, err := clt.doRequest("GET", fmt.Sprintf("/flow/%d", id), nil)
if err != nil {
return
}
flow = new(FlowInfo)
if err = json.Unmarshal(body, flow); err != nil {
return
}
return
}
// CreateFlow creates a new flow using the Controller REST API
func (clt *Client) CreateFlow(name, description string) (*FlowInfo, error) {
response := FlowCreateResponse{}
body, err := clt.doRequest("POST", "/flow", FlowCreateRequest{Name: name, Description: description})
if err != nil {
return nil, err
}
if err := json.Unmarshal(body, &response); err != nil {
return nil, err
}
return clt.GetFlowByID(response.ID)
}
// UpdateFlow patches a flow using the Controller REST API
func (clt *Client) UpdateFlow(request *FlowUpdateRequest) (*FlowInfo, error) {
_, err := clt.doRequest("PATCH", fmt.Sprintf("/flow/%d", request.ID), *request)
if err != nil {
return nil, err
}
return clt.GetFlowByID(request.ID)
}
// StartFlow set the flow as active using the Controller REST API
func (clt *Client) StartFlow(id int) (*FlowInfo, error) {
active := true
return clt.UpdateFlow(&FlowUpdateRequest{ID: id, IsActivated: &active})
}
// StopFlow set the flow as inactive using the Controller REST API
func (clt *Client) StopFlow(id int) (*FlowInfo, error) {
active := false
return clt.UpdateFlow(&FlowUpdateRequest{ID: id, IsActivated: &active})
}
// GetAllFlows retrieve all flows information from the Controller REST API
func (clt *Client) GetAllFlows() (response *FlowListResponse, err error) {
body, err := clt.doRequest("GET", "/flow", nil)
if err != nil {
return
}
response = new(FlowListResponse)
if err = json.Unmarshal(body, response); err != nil {
return
}
return response, nil
}
// GetFlowByName retrieve the flow information by getting all flows then searching for the first occurance in the list
func (clt *Client) GetFlowByName(name string) (_ *FlowInfo, err error) {
list, err := clt.GetAllFlows()
if err != nil {
return
}
for _, flow := range list.Flows {
if flow.Name == name {
return &flow, nil
}
}
return nil, NewNotFoundError(fmt.Sprintf("Could not find flow: %s", name))
}
// DeleteFlow deletes a flow using the Controller REST API
func (clt *Client) DeleteFlow(id int) (err error) {
_, err = clt.doRequest("DELETE", fmt.Sprintf("/flow/%d", id), nil)
return
}