func()

in spinnaker/spinnaker.go [504:549]


func (s Spinnaker) account(name string) (account, error) {
	url := s.accountsURL(true)
	resp, err := s.client.Get(url)
	var ac account

	// Usual HTTP checks
	if err != nil {
		return ac, errors.Wrapf(err, "http get failed at %s", url)
	}

	defer func() {
		if cerr := resp.Body.Close(); cerr != nil && err == nil {
			err = errors.Wrap(err, fmt.Sprintf("body close failed at %s", url))
		}
	}()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return ac, errors.Wrapf(err, "body read failed at %s", url)
	}

	var accounts []account
	err = json.Unmarshal(body, &accounts)
	if err != nil {
		return ac, errors.Wrap(err, "json unmarshal failed")
	}
	statusKO := resp.StatusCode != http.StatusOK

	// Finally find account
	for _, a := range accounts {
		if a.Name != name {
			continue
		}
		if statusKO {
			if a.Error == "" {
				return ac, errors.Errorf("unexpected status code: %d. body: %s", resp.StatusCode, body)
			}

			return ac, errors.Errorf("unexpected status code: %d. error: %s", resp.StatusCode, a.Error)
		}

		return a, nil
	}

	return ac, errors.New("the account name doesn't exist")
}