Skip to content
This repository has been archived by the owner on Dec 5, 2024. It is now read-only.

Commit

Permalink
Migrate to cobra (issue #46)
Browse files Browse the repository at this point in the history
Closes #46
  • Loading branch information
pcantera committed Jul 13, 2021
1 parent cbedb26 commit 584f59e
Show file tree
Hide file tree
Showing 418 changed files with 17,416 additions and 49,525 deletions.
14 changes: 0 additions & 14 deletions .travis.yml

This file was deleted.

5,174 changes: 5,174 additions & 0 deletions api/api.go

Large diffs are not rendered by default.

213 changes: 213 additions & 0 deletions api/api_low_level.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
// Copyright (c) 2017-2021 Ingram Micro Inc.

package api

import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"regexp"
"strings"

log "github.com/sirupsen/logrus"
)

const ContentTypeApplicationJson = "application/json"

// Post sends POST request to Concerto API
func (imco *IMCOClient) Post(path string, payload *map[string]interface{}) ([]byte, int, error) {

url, jsPayload, err := imco.prepareCall(path, payload)
if err != nil {
return nil, 0, err
}

log.Debugf("Sending POST request to %s with payload %v ", url, jsPayload)
req, err := http.NewRequest("POST", url, jsPayload)
req.Header.Add("Content-Type", ContentTypeApplicationJson)
if imco.config.BrownfieldToken != "" {
log.Debugf(
"Including brownfield token %s in POST request as X-Concerto-Brownfield-Token header ",
imco.config.BrownfieldToken,
)
req.Header.Add("X-Concerto-Brownfield-Token", imco.config.BrownfieldToken)
}
if imco.config.CommandPollingToken != "" && imco.config.ServerID != "" {
log.Debugf(
"Including command polling token %s in POST request as X-IMCO-Command-Polling-Token header ",
imco.config.CommandPollingToken,
)
req.Header.Add("X-IMCO-Command-Polling-Token", imco.config.CommandPollingToken)
log.Debugf("Including server id %s in POST request as X-IMCO-server-ID header ", imco.config.ServerID)
req.Header.Add("X-IMCO-server-ID", imco.config.ServerID)
}
response, err := imco.client.Do(req)
if err != nil {
return nil, 0, err
}

return imco.receiveResponse(response)
}

// Put sends PUT request to Concerto API
func (imco *IMCOClient) Put(path string, payload *map[string]interface{}) ([]byte, int, error) {
url, jsPayload, err := imco.prepareCall(path, payload)
if err != nil {
return nil, 0, err
}

log.Debugf("Sending PUT request to %s with payload %v ", url, jsPayload)
request, err := http.NewRequest("PUT", url, jsPayload)
if err != nil {
return nil, 0, err
}
request.Header = map[string][]string{"Content-type": {ContentTypeApplicationJson}}
response, err := imco.client.Do(request)
if err != nil {
return nil, 0, err
}

return imco.receiveResponse(response)
}

// Delete sends DELETE request to Concerto API
func (imco *IMCOClient) Delete(path string) ([]byte, int, error) {
url, _, err := imco.prepareCall(path, nil)
if err != nil {
return nil, 0, err
}

log.Debugf("Sending DELETE request to %s", url)
request, err := http.NewRequest("DELETE", url, nil)
if err != nil {
return nil, 0, err
}
request.Header = map[string][]string{"Content-type": {ContentTypeApplicationJson}}
response, err := imco.client.Do(request)
if err != nil {
return nil, 0, err
}

return imco.receiveResponse(response)
}

// Get sends GET request to Concerto API
func (imco *IMCOClient) Get(path string) ([]byte, int, error) {

url, _, err := imco.prepareCall(path, nil)
if err != nil {
return nil, 0, err
}

log.Debugf("Sending GET request to %s", url)
response, err := imco.client.Get(url)
if err != nil {
return nil, 0, err
}

return imco.receiveResponse(response)
}

// GetFile sends GET request to Concerto API and receives a file
func (imco *IMCOClient) GetFile(url string, filePath string, discoveryFileName bool) (string, int, error) {

log.Debugf("Sending GET request to %s", url)
response, err := imco.client.Get(url)
if err != nil {
return "", 0, err
}

defer response.Body.Close()
log.Debugf("Status code:%d message:%s", response.StatusCode, response.Status)

realFileName := filePath
if discoveryFileName {
r, err := regexp.Compile("filename=\\\"([^\\\"]*){1}\\\"")
if err != nil {
return "", 0, err
}
realFileName = fmt.Sprintf(
"%s/%s",
filePath,
r.FindStringSubmatch(response.Header.Get("Content-Disposition"))[1],
)
}

output, err := os.Create(realFileName)
if err != nil {
return "", response.StatusCode, err
}
defer output.Close()

n, err := io.Copy(output, response.Body)
if err != nil {
return "", response.StatusCode, err
}

log.Debugf("%#v bytes downloaded", n)
return realFileName, response.StatusCode, nil
}

// PutFile sends PUT request to send a file
func (imco *IMCOClient) PutFile(sourceFilePath string, targetURL string) ([]byte, int, error) {

data, err := os.Open(sourceFilePath)
if err != nil {
return nil, 0, err
}

req, err := http.NewRequest("PUT", targetURL, data)
if err != nil {
return nil, 0, err
}

res, err := imco.client.Do(req)
if err != nil {
return nil, 0, err
}
defer res.Body.Close()

buf, err := ioutil.ReadAll(res.Body)
status := res.StatusCode
return buf, status, nil
}

func (imco *IMCOClient) prepareCall(
path string,
payload *map[string]interface{},
) (url string, jsPayload *strings.Reader, err error) {

if imco.config == nil || imco.client == nil {
return "", nil, fmt.Errorf("cannot call web service without loading configuration")
}

url = fmt.Sprintf("%s%s", imco.config.APIEndpoint, path)

if payload == nil {
return url, nil, nil
}

// payload to json
json, err := json.Marshal(payload)
if err != nil {
return "", nil, err
}

return url, strings.NewReader(string(json)), err
}

func (imco *IMCOClient) receiveResponse(response *http.Response) (body []byte, status int, err error) {

defer response.Body.Close()
body, err = ioutil.ReadAll(response.Body)
if err != nil {
return nil, 0, err
}
log.Debugf("Response : %s", body)
log.Debugf("Status code: (%d) %s", response.StatusCode, response.Status)

return body, response.StatusCode, nil
}
46 changes: 46 additions & 0 deletions api/api_mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) 2017-2021 Ingram Micro Inc.

package api

//import "github.com/stretchr/testify/mock"
//
//// MockIMCOClient service manager
//type MockIMCOClient struct {
// mock.Mock
//}
//
//// Post mocks POST request to IMCO API
//func (m *MockIMCOClient) Post(path string, payload *map[string]interface{}) ([]byte, int, error) {
// args := m.Called(path, payload)
// return args.Get(0).([]byte), args.Int(1), args.Error(2)
//}
//
//// Put mocks PUT request to IMCO API
//func (m *MockIMCOClient) Put(path string, payload *map[string]interface{}) ([]byte, int, error) {
// args := m.Called(path, payload)
// return args.Get(0).([]byte), args.Int(1), args.Error(2)
//}
//
//// Delete mocks DELETE request to IMCO API
//func (m *MockIMCOClient) Delete(path string) ([]byte, int, error) {
// args := m.Called(path)
// return args.Get(0).([]byte), args.Int(1), args.Error(2)
//}
//
//// Get mocks GET request to IMCO API
//func (m *MockIMCOClient) Get(path string) ([]byte, int, error) {
// args := m.Called(path)
// return args.Get(0).([]byte), args.Int(1), args.Error(2)
//}
//
//// GetFile sends GET request to IMCO API and receives a file
//func (m *MockIMCOClient) GetFile(url string, filePath string, discoveryFileName bool) (string, int, error) {
// args := m.Called(url, filePath)
// return args.String(0), args.Int(1), args.Error(2)
//}
//
//// PutFile sends PUT request to send a file
//func (m *MockIMCOClient) PutFile(sourceFilePath string, targetURL string) ([]byte, int, error) {
// args := m.Called(sourceFilePath, targetURL)
// return args.Get(0).([]byte), args.Int(1), args.Error(2)
//}
3 changes: 3 additions & 0 deletions api/api_mocked.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Copyright (c) 2017-2021 Ingram Micro Inc.

package api
3 changes: 3 additions & 0 deletions api/api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Copyright (c) 2017-2021 Ingram Micro Inc.

package api
71 changes: 0 additions & 71 deletions api/audit/events_api.go

This file was deleted.

Loading

0 comments on commit 584f59e

Please sign in to comment.