func Handler(request events.CloudWatchEvent) (Response, error) {

	log.Print("complete request")

	//Build the URL.
	data := url.Values{}
	data.Add("client_id", cli)
	data.Add("username", usr)
	data.Add("password", pas)
	data.Add("grant_type", grt)

	u, err := url.ParseRequestURI(apiURL)
	if err != nil {
		log.Printf("error: %v", err)
		return Response{
			Message: err.Error(),
		}, err
	}

	u.Path = "/CherwellAPI/token"

	fmt.Printf("Server URL: %v", u.String())
	client := &http.Client{}

	//Build the The Auth request.
	r, err := http.NewRequest("POST", u.String(), strings.NewReader(data.Encode()))

	if err != nil {
		log.Printf("error: %v", err)
		return Response{
			Message: err.Error(),
		}, err
	}

	r.Header.Add("Content-Type", "application/x-www-form-urlencoded")
	r.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))

	//Make the Auth Request
	resp, err := client.Do(r)

	if err != nil {
		log.Printf("error: %v", err)
		return Response{
			Message: err.Error(),
		}, err
	}
	defer resp.Body.Close()

	rt := ResponseToken{}

	rtData, err := ioutil.ReadAll(resp.Body)

	if err != nil {
		log.Printf("error: %v", err)
		return Response{
			Message: err.Error(),
		}, err
	}

	json.Unmarshal(rtData, &rt)

	rd, err := request.Detail.MarshalJSON()

	if err != nil {
		log.Printf("error: %v", err)
		return Response{
			Message: err.Error(),
		}, err
	}

	//Build the BusiObject slice.
	busID = "943e3fc2a8dab7f22f1d174bdf8e0c7010681362ec"
	f1 := field{
		Dirty:   true,
		FieldID: "BO:943e3fc2a8dab7f22f1d174bdf8e0c7010681362ec,FI:943e3fc42d204079d03a2947f9840e4c6f45884f35",
		Value:   string(rd),
	}

	var f1s []field
	f1s = append(f1s, f1)

	iD1 := Mainevent{
		BusObID: busID,
		Fields:  f1s,
		Persist: true,
	}

	jsonStr, err := json.Marshal(iD1)
	if err != nil {
		log.Printf("error: %v", err)
		return Response{
			Message: err.Error(),
		}, err
	}
	log.Printf("Cherwell request: %v", iD1)

	//Make a request and send event info to Cherwell.
	freq, err := http.NewRequest("POST", apiURL+"/CherwellAPI/api/V1/savebusinessobject", bytes.NewBuffer(jsonStr))
	if err != nil {
		log.Printf("error: %v", err)
		return Response{
			Message: err.Error(),
		}, err
	}

	freq.Header.Set("Content-Type", "application/json")
	freq.Header.Set("Authorization", "Bearer "+rt.AccessToken)

	fresp, err := client.Do(freq)
	if err != nil {
		log.Printf("error: %v", err)
		return Response{
			Message: err.Error(),
		}, err
	}

	fmt.Printf("Cherwell response: %v", fresp)
	defer fresp.Body.Close()

	fData, err := ioutil.ReadAll(fresp.Body)

	if err != nil {
		log.Printf("error: %v", err)
		return Response{
			Message: err.Error(),
		}, err
	}

	return Response{
		Message: string(fData),
	}, nil

}
//Handler for the Lambda request
Handler for the Lambda request
// Handler for the Lambda request
func Handler(request events.CloudWatchEvent) (Response, error) {
	log.Print("complete request")
	data := url.Values{}
	data.Add("client_id", cli)
	data.Add("username", usr)
	data.Add("password", pas)
	data.Add("grant_type", grt)
	u, err := url.ParseRequestURI(apiURL)
	if err != nil {
		log.Printf("error: %v", err)
		return Response{
			Message: err.Error(),
		}, err
	}
	u.Path = "/CherwellAPI/token"
	fmt.Printf("Server URL: %v", u.String())
	client := &http.Client{}
	r, err := http.NewRequest("POST", u.String(), strings.NewReader(data.Encode()))
	if err != nil {
		log.Printf("error: %v", err)
		return Response{
			Message: err.Error(),
		}, err
	}
	r.Header.Add("Content-Type", "application/x-www-form-urlencoded")
	r.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))
	resp, err := client.Do(r)
	if err != nil {
		log.Printf("error: %v", err)
		return Response{
			Message: err.Error(),
		}, err
	}
	defer resp.Body.Close()
	rt := ResponseToken{}
	rtData, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Printf("error: %v", err)
		return Response{
			Message: err.Error(),
		}, err
	}
	json.Unmarshal(rtData, &rt)
	rd, err := request.Detail.MarshalJSON()
	if err != nil {
		log.Printf("error: %v", err)
		return Response{
			Message: err.Error(),
		}, err
	}
	busID = "943e3fc2a8dab7f22f1d174bdf8e0c7010681362ec"
	f1 := field{
		Dirty:   true,
		FieldID: "BO:943e3fc2a8dab7f22f1d174bdf8e0c7010681362ec,FI:943e3fc42d204079d03a2947f9840e4c6f45884f35",
		Value:   string(rd),
	}
	var f1s []field
	f1s = append(f1s, f1)
	iD1 := Mainevent{
		BusObID: busID,
		Fields:  f1s,
		Persist: true,
	}
	jsonStr, err := json.Marshal(iD1)
	if err != nil {
		log.Printf("error: %v", err)
		return Response{
			Message: err.Error(),
		}, err
	}
	log.Printf("Cherwell request: %v", iD1)
	freq, err := http.NewRequest("POST", apiURL+"/CherwellAPI/api/V1/savebusinessobject", bytes.NewBuffer(jsonStr))
	if err != nil {
		log.Printf("error: %v", err)
		return Response{
			Message: err.Error(),
		}, err
	}
	freq.Header.Set("Content-Type", "application/json")
	freq.Header.Set("Authorization", "Bearer "+rt.AccessToken)
	fresp, err := client.Do(freq)
	if err != nil {
		log.Printf("error: %v", err)
		return Response{
			Message: err.Error(),
		}, err
	}
	fmt.Printf("Cherwell response: %v", fresp)
	defer fresp.Body.Close()
	fData, err := ioutil.ReadAll(fresp.Body)
	if err != nil {
		log.Printf("error: %v", err)
		return Response{
			Message: err.Error(),
		}, err
	}
	return Response{
		Message: string(fData),
	}, nil
}
func Handler ( request events . CloudWatchEvent ) ( Response , error ) { log . Print ( " " ) data := url . Values { } data . Add ( " " , cli ) data . Add ( " " , usr ) data . Add ( " " , pas ) data . Add ( " " , grt ) u , err := url . ParseRequestURI ( apiURL ) if err != nil { log . Printf ( " " , err ) return Response { Message : err . Error ( ) , } , err } u . Path = " " fmt . Printf ( " " , u . String ( ) ) client := & http . Client { } r , err := http . NewRequest ( " " , u . String ( ) , strings . NewReader ( data . Encode ( ) ) ) if err != nil { log . Printf ( " " , err ) return Response { Message : err . Error ( ) , } , err } r . Header . Add ( " " , " " ) r . Header . Add ( " " , strconv . Itoa ( len ( data . Encode ( ) ) ) ) resp , err := client . Do ( r ) if err != nil { log . Printf ( " " , err ) return Response { Message : err . Error ( ) , } , err } defer resp . Body . Close ( ) rt := ResponseToken { } rtData , err := ioutil . ReadAll ( resp . Body ) if err != nil { log . Printf ( " " , err ) return Response { Message : err . Error ( ) , } , err } json . Unmarshal ( rtData , & rt ) rd , err := request . Detail . MarshalJSON ( ) if err != nil { log . Printf ( " " , err ) return Response { Message : err . Error ( ) , } , err } busID = " " f1 := field { Dirty : true , FieldID : " " , Value : string ( rd ) , } var f1s [ ] field f1s = append ( f1s , f1 ) iD1 := Mainevent { BusObID : busID , Fields : f1s , Persist : true , } jsonStr , err := json . Marshal ( iD1 ) if err != nil { log . Printf ( " " , err ) return Response { Message : err . Error ( ) , } , err } log . Printf ( " " , iD1 ) freq , err := http . NewRequest ( " " , apiURL + " " , bytes . NewBuffer ( jsonStr ) ) if err != nil { log . Printf ( " " , err ) return Response { Message : err . Error ( ) , } , err } freq . Header . Set ( " " , " " ) freq . Header . Set ( " " , " " + rt . AccessToken ) fresp , err := client . Do ( freq ) if err != nil { log . Printf ( " " , err ) return Response { Message : err . Error ( ) , } , err } fmt . Printf ( " " , fresp ) defer fresp . Body . Close ( ) fData , err := ioutil . ReadAll ( fresp . Body ) if err != nil { log . Printf ( " " , err ) return Response { Message : err . Error ( ) , } , err } return Response { Message : string ( fData ) , } , nil }
Handler for the Lambda request
Handler for the Lambda request
//Build the URL. //Build the The Auth request. //Make the Auth Request //Build the BusiObject slice. //Make a request and send event info to Cherwell.