コード例 #1
0
func (cli *OpsGenieAlertClient) AttachFile(req alerts.AttachFileAlertRequest) (*alerts.AttachFileAlertResponse, error) {
	req.ApiKey = cli.apiKey
	// validate the mandatory parameters: apiKey, alertId/alias, attachment
	if req.ApiKey == "" {
		return nil, errors.New("ApiKey is a mandatory field and can not be empty.")
	}
	if req.Attachment == "" {
		return nil, errors.New("Attachment is a mandatory field and can not be empty.")
	}
	if req.AlertId == "" && req.Alias == "" {
		return nil, errors.New("At least either Alert Id or Alias should be set in the request.")
	}
	if req.AlertId != "" && req.Alias != "" {
		return nil, errors.New("Either Alert Id or Alias should be set in the request not both.")
	}

	var b bytes.Buffer
	w := multipart.NewWriter(&b)

	fileName := req.Attachment
	file, err := os.Open(fileName)
	if err != nil {
		return nil, errors.New("Attachment can not be opened for reading.")
	}
	// add the attachment
	fw, err := w.CreateFormFile("attachment", fileName)
	if err != nil {
		return nil, errors.New("Can not build the request with the field attachment.")
	}
	if _, err := io.Copy(fw, file); err != nil {
		return nil, errors.New("Can not copy the attachment into the request.")
	}

	defer file.Close()

	// Add the other fields
	// empty fields should not be placed into the request
	// otherwise it yields an incomplete boundary exception
	if req.ApiKey != "" {
		if fw, err = w.CreateFormField("apiKey"); err != nil {
			return nil, errors.New("Can not build the request with the field apiKey.")
		}
		if _, err = fw.Write([]byte(req.ApiKey)); err != nil {
			return nil, errors.New("Can not write the ApiKey value into the request.")
		}
	}
	if req.AlertId != "" {
		if fw, err = w.CreateFormField("alertId"); err != nil {
			return nil, errors.New("Can not build the request with the field alertId.")
		}
		if _, err = fw.Write([]byte(req.AlertId)); err != nil {
			return nil, errors.New("Can not write the AlertId value into the request.")
		}
	}
	if req.Alias != "" {
		if fw, err = w.CreateFormField("alias"); err != nil {
			return nil, errors.New("Can not build the request with the field alias.")
		}
		if _, err = fw.Write([]byte(req.Alias)); err != nil {
			return nil, errors.New("Can not write the Alias value into the request.")
		}
	}
	if req.User != "" {
		if fw, err = w.CreateFormField("user"); err != nil {
			return nil, errors.New("Can not build the request with the field user.")
		}
		if _, err = fw.Write([]byte(req.User)); err != nil {
			return nil, errors.New("Can not write the User value into the request.")
		}
	}
	if req.Source != "" {
		if fw, err = w.CreateFormField("source"); err != nil {
			return nil, errors.New("Can not build the request with the field source.")
		}
		if _, err = fw.Write([]byte(req.Source)); err != nil {
			return nil, errors.New("Can not write the Source value into the request.")
		}
	}
	if req.IndexFile != "" {
		if fw, err = w.CreateFormField("indexFile"); err != nil {
			return nil, errors.New("Can not build the request with the field indexFile.")
		}
		if _, err = fw.Write([]byte(req.IndexFile)); err != nil {
			return nil, errors.New("Can not write the IndexFile value into the request.")
		}
	}
	if req.Note != "" {
		if fw, err = w.CreateFormField("note"); err != nil {
			return nil, errors.New("Can not build the request with the field note.")
		}
		if _, err = fw.Write([]byte(req.Note)); err != nil {
			return nil, errors.New("Can not write the Note value into the request.")
		}
	}

	w.Close()

	httpReq, err := http.NewRequest("POST", ATTACH_FILE_ALERT_URL, &b)
	if err != nil {
		return nil, errors.New("Can not create the multipart/form-data request.")
	}
	httpReq.Header.Set("Content-Type", w.FormDataContentType())
	client := &http.Client{}
	// proxy settings
	if cli.proxy != "" {
		proxyUrl, proxyErr := url.Parse(cli.proxy)
		if proxyErr != nil {
			return nil, errors.New("Can not set the proxy configuration")
		}
		client.Transport = &http.Transport{Proxy: http.ProxyURL(proxyUrl)}
	}
	var res *http.Response
	for i := 0; i < cli.retries; i++ {
		res, err = client.Do(httpReq)
		if err == nil {
			break
		}
		time.Sleep(TIME_SLEEP_BETWEEN_REQUESTS)
	}

	if err != nil {
		return nil, errors.New("Can not attach the file, unable to send the request.")
	}

	// check the returning HTTP status code
	httpStatusCode := res.StatusCode
	if httpStatusCode >= 400 && httpStatusCode < 500 {
		return nil, errors.New(fmt.Sprintf("Client error %d returned", httpStatusCode))
	}
	if httpStatusCode >= 500 {
		return nil, errors.New(fmt.Sprintf("Server error %d returned", httpStatusCode))
	}

	attachFileAlertResp := alerts.AttachFileAlertResponse{Status: res.Status, Code: res.StatusCode}
	return &attachFileAlertResp, nil
}