forked from eclipse-iofog/iofog-go-sdk
/
templates.go
157 lines (137 loc) · 4.22 KB
/
templates.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* *******************************************************************************
* * 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 (
"bytes"
"encoding/json"
"fmt"
"io"
"mime/multipart"
)
func (clt *Client) IsApplicationTemplateCapable() error {
if _, err := clt.doRequest("HEAD", "/capabilities/applicationTemplates", nil); err != nil {
// If 404, not capable
if _, ok := err.(*NotFoundError); ok {
return NewNotSupportedError("Application Templates")
}
return err
}
return nil
}
func (clt *Client) applicationTemplatePreflight() error {
// Check capability
if err := clt.IsApplicationTemplateCapable(); err != nil {
return err
}
if !clt.isLoggedIn() {
return NewError(edgeResourceLoggedInErr)
}
return nil
}
// CreateApplicationTemplateFromYAML creates a new application template using the Controller REST API
// It sends the yaml file to Controller REST API
func (clt *Client) CreateApplicationTemplateFromYAML(file io.Reader) (*ApplicationTemplate, error) {
requestBody := &bytes.Buffer{}
writer := multipart.NewWriter(requestBody)
part, _ := writer.CreateFormFile("template", "application.yaml")
_, err := io.Copy(part, file)
if err != nil {
return nil, err
}
writer.Close()
headers := map[string]string{
"Content-Type": writer.FormDataContentType(),
}
body, err := clt.doRequestWithHeaders("POST", "/applicationTemplate/yaml", requestBody, headers)
if err != nil {
return nil, err
}
response := ApplicationTemplateCreateResponse{}
if err := json.Unmarshal(body, &response); err != nil {
return nil, err
}
return clt.GetApplicationTemplate(response.Name)
}
// UpdateApplicationTemplateFromYAML updates an application template using the Controller REST API
// It sends the yaml file to Controller REST API
func (clt *Client) UpdateApplicationTemplateFromYAML(name string, file io.Reader) (*ApplicationTemplate, error) {
requestBody := &bytes.Buffer{}
writer := multipart.NewWriter(requestBody)
part, _ := writer.CreateFormFile("template", "microservice.yaml")
_, err := io.Copy(part, file)
if err != nil {
return nil, err
}
writer.Close()
headers := map[string]string{
"Content-Type": writer.FormDataContentType(),
}
_, err = clt.doRequestWithHeaders("PUT", fmt.Sprintf("/applicationTemplate/yaml/%s", name), requestBody, headers)
if err != nil {
return nil, err
}
return clt.GetApplicationTemplate(name)
}
func (clt *Client) UpdateApplicationTemplateMetadata(name string, newMeta *ApplicationTemplateMetadataUpdateRequest) error {
if err := clt.applicationTemplatePreflight(); err != nil {
return err
}
// Run request
url := fmt.Sprintf("/applicationTemplate/%s", name)
if _, err := clt.doRequest("PATCH", url, newMeta); err != nil {
return err
}
return nil
}
func (clt *Client) ListApplicationTemplates() (*ApplicationTemplateListResponse, error) {
if err := clt.applicationTemplatePreflight(); err != nil {
return nil, err
}
// Run request
response := ApplicationTemplateListResponse{}
body, err := clt.doRequest("GET", "/applicationTemplates", nil)
if err != nil {
return nil, err
}
if err := json.Unmarshal(body, &response); err != nil {
return nil, err
}
return &response, nil
}
func (clt *Client) GetApplicationTemplate(name string) (*ApplicationTemplate, error) {
if err := clt.applicationTemplatePreflight(); err != nil {
return nil, err
}
// Run request
response := ApplicationTemplate{}
url := fmt.Sprintf("/applicationTemplate/%s", name)
body, err := clt.doRequest("GET", url, nil)
if err != nil {
return nil, err
}
if err := json.Unmarshal(body, &response); err != nil {
return nil, err
}
return &response, nil
}
func (clt *Client) DeleteApplicationTemplate(name string) error {
if err := clt.applicationTemplatePreflight(); err != nil {
return err
}
// Run request
url := fmt.Sprintf("/applicationTemplate/%s", name)
if _, err := clt.doRequest("DELETE", url, nil); err != nil {
return err
}
return nil
}