Exemple #1
0
// PutResponse returns an http.Response object for the updated resource
func (s *ScalewayAPI) PutResponse(resource string, data interface{}) (*http.Response, error) {
	uri := fmt.Sprintf("%s/%s", strings.TrimRight(s.APIUrl, "/"), resource)
	payload := new(bytes.Buffer)
	encoder := json.NewEncoder(payload)
	if err := encoder.Encode(data); err != nil {
		return nil, err
	}

	req, err := http.NewRequest("PUT", uri, payload)
	if err != nil {
		return nil, err
	}
	req.Header.Set("X-Auth-Token", s.Token)
	req.Header.Set("Content-Type", "application/json")

	curl, err := http2curl.GetCurlCommand(req)
	if err != nil {
		return nil, err
	}
	if os.Getenv("SCW_SENSITIVE") != "1" {
		log.Debug(s.HideAPICredentials(curl.String()))
	} else {
		log.Debug(curl.String())
	}

	return s.client.Do(req)
}
Exemple #2
0
// DeleteResponse returns an http.Response object for the deleted resource
func (s *ScalewayAPI) DeleteResponse(resource string) (*http.Response, error) {
	uri := fmt.Sprintf("%s/%s", strings.TrimRight(s.APIUrl, "/"), resource)

	req, err := http.NewRequest("DELETE", uri, nil)
	if err != nil {
		return nil, err
	}
	req.Header.Set("X-Auth-Token", s.Token)
	req.Header.Set("Content-Type", "application/json")

	curl, err := http2curl.GetCurlCommand(req)
	if os.Getenv("SCW_SENSITIVE") != "1" {
		log.Debug(s.HideAPICredentials(curl.String()))
	} else {
		log.Debug(curl.String())
	}

	return s.client.Do(req)
}
Exemple #3
0
// PatchUserdata sets a user data
func (s *ScalewayAPI) PatchUserdata(serverID string, key string, value []byte) error {
	var resource string
	if s.isMetadata {
		resource = fmt.Sprintf("/user_data/%s", key)
	} else {
		resource = fmt.Sprintf("servers/%s/user_data/%s", serverID, key)
	}

	uri := fmt.Sprintf("%s/%s", strings.TrimRight(s.APIUrl, "/"), resource)
	payload := new(bytes.Buffer)
	payload.Write(value)

	req, err := http.NewRequest("PATCH", uri, payload)
	if err != nil {
		return err
	}

	req.Header.Set("X-Auth-Token", s.Token)
	req.Header.Set("Content-Type", "text/plain")

	curl, err := http2curl.GetCurlCommand(req)
	if os.Getenv("SCW_SENSITIVE") != "1" {
		log.Debug(s.HideAPICredentials(curl.String()))
	} else {
		log.Debug(curl.String())
	}

	resp, err := s.client.Do(req)
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	if resp.StatusCode == 204 {
		return nil
	}

	return fmt.Errorf("cannot set user_data (%d)", resp.StatusCode)
}