0

I'm writing go code to access cloudfoundry platform and extract summary of data for all the apps that have been pushed on the cloud.

I can access every app individually and display the data related to each through an http.GET request but I want to be able to update/change some of the data specific to an app. This is sample data returned for one app named xzys:

{
   "metadata": {
      "guid": "71a3c77f-d2791232323-4b7625dq32908492b04f17e",
      "url": "/v2/apps/71a3c77f-d2791232323-4b7625dq32908492b04f17e",
      "created_at": "2000-18-24T",
      "updated_at": "2000-18-27T"
   },
   "entity": {
      "name": "xzys",
      "production": false,
      "space_guid": "65050bcb-81c6-45a2-b6ef-5e7097c7ece1",
      "stack_guid": "89a4fb19-08ef-4c44-90f7-f222a5699fdc",
      "buildpack": null,
      "detected_buildpack": "null,
      "environment_json": {},
      "memory": 1024,
      "instances": 3,
      "disk_quota": 512,
      "state": "STARTED",
      "version": "67124c27-9958-45a7-afc5-5b27007348ab",
      "command": null,
      "console": false,
      "debug": null,
      "staging_task_id": "e901672f958b4ff39d4efb48290367e8",
      "package_state": "STAGED",
      "health_check_timeout": null,
      "staging_failed_reason": null,
      "docker_image": null,
      "package_updated_at": "2014-10-24T12:53:25+00:00",
      "space_url": "/v2/spaces/71a3c77f-d2791232323-4b7625dq32908492b04f17e",
      "stack_url": "/v2/stacks/71a3c77f-d2791232323-4b7625dq32908492b04f17e",
      "events_url": "/v2/apps/71a3c77f-d2791232323-4b7625dq32908492b04f17e/events",
      "service_bindings_url": "/v2/apps/7-4b7625dq32908492b04f17e/service_bindings",
      "routes_url": "/v2/apps/771a3c77f-d2791232323-4b7625dq32908492b04f17e/routes"
   }
}

So I have a url (/v2/apps/71a3c77f-d2791232323-4b7625dq32908492b04f17e) that contains the summary of info for an individual app. I've been using ahttp.Postform()` to this url to update just one of the resources of this app (I'm aiming to update the number of Instances for this app).

This is my code for updating the "Instances" resource of my app:

func UpdateInstances(token string){

    tr := &http.Transport{
        TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
    }

    client := &http.Client{Transport: tr}

    req, err := client.PostForm("/v2/apps/71a3c77f-d2791232323-4b7625dq32908492b04f17e", url.Values{"instances" : 4})
    req.Header.Add("Authorization", token)
    if err != nil {
        log.Fatal(err)
    }
    defer req.Body.Close()
}

This doesn't give me any errors but also doesn't update the resource.

the swine
  • 10,713
  • 7
  • 58
  • 100
Anupma Raj
  • 9
  • 2
  • 5

2 Answers2

0

http://golang.org/pkg/net/url/#Values

type Values map[string][]string

You see that Values is a map with keys that are of type string and values that are []string.

req, err := client.PostForm("/v2/apps/71a3c77f-d2791232323-4b7625dq32908492b04f17e", url.Values{"instances" : 4})

So you have it all fine, right up until the value, when you try and pass in a number, where it expects an []string. Change it to

client.PostForm("/v2/apps/71a3c77f-d2791232323-4b7625dq32908492b04f17e",
                  url.Values{ "instances": { "4" }})

Or you could do

v := url.Values{}
v.Set("instances", "4")
client.PostForm("/v2/apps/71a3c77f-d2791232323-4b7625dq32908492b04f17e", v)

And it should work

dave
  • 62,300
  • 5
  • 72
  • 93
  • Hey! Thanks for your answer. I tried what you suggested, and although it didnt give me any errors, it failed to update the instances :( Since instances are float64, I tried updating another resource thats actually a string called "name" but that didn't work either. Any other suggestions? – Anupma Raj Oct 29 '14 at 10:04
  • I'm not familiar with cloudfoundry - can you show a curl call that works in updating the value and we can figure out what is different from what you are sending? – dave Oct 29 '14 at 17:07
  • Hey @Dave, Thanks I solved the problem. Just needed to use the method "Put" in a http.NewRequest – Anupma Raj Oct 30 '14 at 16:30
-3
func UpdateInstances(token string) {
    tr := &http.Transport{
        TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
    }
   client := &http.Client{Transport: tr}

    //Displayed a form and used input value as "instances"
    instStr := r.FormValue("instances")
    inst, err := strconv.Atoi(instStr)//because the put request requires an integer
    if err != nil {
        panic(err)
    }

   type command struct {
        Instances int `json:"instances"`
    }

    bb, err := json.Marshal(&command{Instances: inst})
    if err != nil {
        log.Fatal(err)
    }

    url := "https://asjdklskjfkdsf.com"

    req, err := http.NewRequest("PUT", url, bytes.NewBuffer(bb))
    req.Header.Add("Authorization", token)
    if err != nil {
        log.Fatal(err)
    }
    defer req.Body.Close()
}
Anupma Raj
  • 9
  • 2
  • 5