Esempio n. 1
0
// GetWebServiceData does the request to the webservice once and get back the data
func (a API) GetWebServiceData() ([]map[string]interface{}, bool, error) {

	notFinish := false
	var err error

	cred, err := strategy.GetCredential("service", "foreign")
	if err != nil {
		log.Error(uuid, "api.getwebservicedata", err, "Getting credentials with API")
	}

	credA, ok := cred.(str.CredentialService)
	if !ok {
		log.Error(uuid, "api.getwebservicedata", err, "Asserting type.")
	}

	userAgent := credA.GetUserAgent()
	method := "GET"

	response, err := webservice.DoRequest(uuid, userAgent, method, a.Connection, nil)
	if err != nil {
		log.Error(uuid, "api.getwebservicedata", err, "Calling Web Service %s.", a.Connection)
		return nil, notFinish, err
	}

	var d map[string]interface{}

	// Use json.Decode for reading streams of JSON data
	if err = json.NewDecoder(strings.NewReader(string(response.Body))).Decode(&d); err != nil {
		log.Error(uuid, "api.getwebservicedata", err, "Decoding data from API.")
	}

	recordsField := credA.GetRecordsFieldName()

	records, ok := d[recordsField].([]interface{}) //this are all the entries
	if !ok {
		log.Error(uuid, "api.getwebservicedata", err, "Asserting type.")
	}

	r := make([]map[string]interface{}, len(records))
	for _, i := range records { // all the entries in the type we need
		r = append(r, i.(map[string]interface{}))
	}

	flattenData, err := flattenizeData(r)
	if err != nil {
		log.Error(uuid, "api.getwebservicedata", err, "Normalizing data from api to fit into fiddler.")
		return nil, notFinish, err
	}

	return flattenData, notFinish, err
}
Esempio n. 2
0
// AddRow send the row to pillar based on which collection is
func AddRow(data map[string]interface{}, tableName string) error {

	var err error

	if _, ok := endpoints[tableName]; ok {

		d, err := json.Marshal(data)
		if err != nil {
			log.Error(uuid, "coral.Addrow", err, "Marshalling %v.", data)
		}

		userAgent := fmt.Sprintf("Sponge Publisher %s.", str.Name)
		_, err = webservice.DoRequest(uuid, userAgent, methodPost, endpoints[tableName], bytes.NewBuffer(d))
		if err != nil {
			log.Error(uuid, "coral.Addrow", err, "Sending request to PILLAR with %v.", data)
		}
	} else {
		err = fmt.Errorf("No information about %s in the available endpoints.", tableName)
	}

	return err
}
Esempio n. 3
0
// CreateIndex calls the service to create index
func CreateIndex(collection string) error {

	var err error

	// get index
	is := str.GetIndexBy(collection) // []map[string]interface{}

	if is == nil {
		err = fmt.Errorf("%s does not exist", collection)
	}

	// get Endpoint
	createIndexURL := str.GetPillarEndpoints()["index"]

	indexes := make([]map[string]interface{}, len(is))
	for i := range is {
		indexes[i] = make(map[string]interface{})
		indexes[i]["target"] = collection
		indexes[i]["index"] = is[i]

		var data []byte
		data, err = json.Marshal(indexes[i])
		if err != nil {
			log.Error(uuid, "coral.createIndex", err, "Marshal index information.")
		}

		userAgent := fmt.Sprintf("Sponge Publisher %s.", str.Name)
		_, err = webservice.DoRequest(uuid, userAgent, methodPost, createIndexURL, bytes.NewBuffer(data))
		if err != nil {
			log.Error(uuid, "coral.createIndex", err, "Sending request to create Index to Pillar.")
		}

	}

	return err
}