I am attempting a 2 part API call to a service for openshift. The first call enables me to GET the template data for the POD we are creating and the second call will call the POST command that leverage the results from the GET within the payload as shown below. The GET returns a large json object. When I get the results from the GET command, I write the results into ioutil.ReadAll() which works fine. I then convert to a string using string() and attempt to use that as the string parameter for "template". The problem that I am facing is that the string parameter contains escape characters. i.e. "{\"kind\":\"Template\",\"apiVersion\":\"template.openshift.io/v1\"...
I can make this work with bash scripts but trying to eliminate the calls to scripts in my code.
BASH SCRIPT EXAMPLE provided by Openshift
$ curl -k \
-X POST \
-d @- \
-H "Authorization: Bearer $TOKEN" \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
https://$ENDPOINT/apis/template.openshift.io/v1/namespaces/$NAMESPACE/templateinstances <<EOF
{
"kind": "TemplateInstance",
"apiVersion": "template.openshift.io/v1",
"metadata": {
"name": "templateinstance"
},
"spec": {
"secret": {
"name": "secret"
},
"template": $(curl -k \
-H "Authorization: Bearer $TOKEN" \
-H 'Accept: application/json' \
https://$ENDPOINT/apis/template.openshift.io/v1/namespaces/openshift/templates/cakephp-mysql-example)
}
}
EOF
Example Go code...
func main() {
b, res := getTemplateData()
if !b{
fmt.Println(res)
os.Exit(1)
}else{
createTemplate(res)
}
}
type templateInstance struct{
Kind string `json:"kind"`
ApiVersion string `json:"apiVersion"`
Metadata MetaData `json:"metadata"`
Spec spec `json:"spec"`
}
type spec struct {
Secret Tsecret `json:"secret"`
Template string `json:"template"`
}
type Tsecret struct{
Name string `json:"name"`
}
func createTemplate(t string) {
var md MetaData
var sp spec
var ts Tsecret
md.Name = "template_name"
ts.Name = "secret_name"
sp.Secret = ts
sp.Template = t
var ti = templateInstance {
Kind: "TemplateInstance",
ApiVersion: "template.openshift.io/v1",
Metadata: md,
Spec: sp,
}
templateData, _ := json.Marshal(ti)
fmt.Println(string(templateData))
}
func getTemplateData() (bool, string){
URL := "https://example.com/apis/template.openshift.io/v1/namespaces/mynamespace/templates/mytemplate"
client := &http.Client{
Timeout:5*time.Second,
Transport: &http.Transport{
Proxy:nil,
TLSClientConfig: &tls.Config{InsecureSkipVerify:true},
},
}
req, err := http.NewRequest("GET", URL, nil)
if err != nil{
fmt.Println(err)
os.Exit(0)
}
req.Header.Add("Accept", "application/json")
req.Header.Add("Authorization", myToken)
req.Header.Add("cache-control", "no-cache")
res, err := client.Do(req)
if err != nil{
fmt.Println("client.Do error")
fmt.Println(err)
return false, "Error calling get template"
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil{
return false, "Error reading Template result"
}else{
return true, string(body)
//string(body) gets used as the string value for "template" parameter of the payload in the bash script example...
}
}
getTemplateData()
and makes the second request. – Cerise Limón