// EnableAction enables an integration/policy according to the --type parameter at OpsGenie.
func EnableAction(c *gcli.Context) {
	val, _ := getVal("type", c)
	switch val {
	case "policy":
		cli, err := NewPolicyClient(c)
		if err != nil {
			os.Exit(1)
		}

		req := policy.EnablePolicyRequest{}
		if val, success := getVal("id", c); success {
			req.ID = val
		}
		if val, success := getVal("name", c); success {
			req.Name = val
		}
		printVerboseMessage("Enable policy request prepared from flags, sending request to OpsGenie..")
		_, err = cli.Enable(req)
		if err != nil {
			fmt.Printf("%s\n", err.Error())
			os.Exit(1)
		}
		fmt.Printf("Policy enabled successfuly\n")

	case "integration":
		cli, err := NewIntegrationClient(c)
		if err != nil {
			os.Exit(1)
		}

		req := integration.EnableIntegrationRequest{}
		if val, success := getVal("id", c); success {
			req.ID = val
		}
		if val, success := getVal("name", c); success {
			req.Name = val
		}
		printVerboseMessage("Enable integration request prepared from flags, sending request to OpsGenie..")
		_, err = cli.Enable(req)
		if err != nil {
			fmt.Printf("%s\n", err.Error())
			os.Exit(1)
		}
		fmt.Printf("Integration enabled successfuly\n")
	default:
		fmt.Printf("Invalid type option %s, specify either integration or policy\n", val)
		gcli.ShowCommandHelp(c, "enable")
		os.Exit(1)
	}
}
// Enable method enables an Policy at OpsGenie.
func (cli *OpsGeniePolicyClient) Enable(req policy.EnablePolicyRequest) (*policy.EnablePolicyResponse, error) {
	req.APIKey = cli.apiKey
	resp, err := cli.sendRequest(cli.buildPostRequest(enablePolicyURL, req))

	if resp == nil {
		return nil, err
	}
	defer resp.Body.Close()

	var enablePolicyResp policy.EnablePolicyResponse
	if err = resp.Body.FromJsonTo(&enablePolicyResp); err != nil {
		message := "Server response can not be parsed, " + err.Error()
		logging.Logger().Warn(message)
		return nil, errors.New(message)
	}
	return &enablePolicyResp, nil
}
Пример #3
0
func (cli *OpsGeniePolicyClient) Enable(req policy.EnablePolicyRequest) (*policy.EnablePolicyResponse, error) {
	req.ApiKey = cli.apiKey
	// validate mandatory fields: id/name, apiKey
	if req.ApiKey == "" && req.Id == "" {
		return nil, errors.New("Api Key or Id should be provided")
	}
	if req.ApiKey != "" && req.Id != "" {
		return nil, errors.New("Either Api Key or Id should be provided, not both")
	}
	// send the request
	var resp *goreq.Response
	var err error
	for i := 0; i < cli.retries; i++ {
		resp, err = cli.buildRequest("POST", ENABLE_POLICY_URL, req).Do()
		if err == nil {
			break
		}
		time.Sleep(TIME_SLEEP_BETWEEN_REQUESTS)
	}
	if err != nil {
		return nil, errors.New("Can not enable the policy, unable to send the request")
	}
	// check for the returning http status, 4xx: client errors, 5xx: server errors
	statusCode := resp.StatusCode
	if statusCode >= 400 && statusCode < 500 {
		return nil, errors.New(fmt.Sprintf("Client error %d occured", statusCode))
	}
	if statusCode >= 500 {
		return nil, errors.New(fmt.Sprintf("Server error %d occured", statusCode))
	}
	// try to parse the returning JSON into the response
	var enablePolicyResp policy.EnablePolicyResponse
	if err = resp.Body.FromJsonTo(&enablePolicyResp); err != nil {
		return nil, errors.New("Server response can not be parsed")
	}
	// parsed successfuly with no errors
	return &enablePolicyResp, nil
}