github.com/SAP/jenkins-library@v1.362.0/cmd/apiProviderList.go (about) 1 package cmd 2 3 import ( 4 "fmt" 5 "net/http" 6 7 "github.com/SAP/jenkins-library/pkg/apim" 8 "github.com/SAP/jenkins-library/pkg/cpi" 9 piperhttp "github.com/SAP/jenkins-library/pkg/http" 10 "github.com/SAP/jenkins-library/pkg/log" 11 "github.com/SAP/jenkins-library/pkg/telemetry" 12 "github.com/pkg/errors" 13 ) 14 15 func apiProviderList(config apiProviderListOptions, telemetryData *telemetry.CustomData, commonPipelineEnvironment *apiProviderListCommonPipelineEnvironment) { 16 httpClient := &piperhttp.Client{} 17 err := runApiProviderList(&config, telemetryData, httpClient, commonPipelineEnvironment) 18 if err != nil { 19 log.Entry().WithError(err).Fatal("step execution failed") 20 } 21 } 22 23 func runApiProviderList(config *apiProviderListOptions, telemetryData *telemetry.CustomData, httpClient piperhttp.Sender, commonPipelineEnvironment *apiProviderListCommonPipelineEnvironment) error { 24 apimData := apim.Bundle{APIServiceKey: config.APIServiceKey, Client: httpClient} 25 err := apim.Utils.InitAPIM(&apimData) 26 if err != nil { 27 return err 28 } 29 return getApiProviderList(config, apimData, commonPipelineEnvironment) 30 } 31 32 func getApiProviderList(config *apiProviderListOptions, apistruct apim.Bundle, commonPipelineEnvironment *apiProviderListCommonPipelineEnvironment) error { 33 httpClient := apistruct.Client 34 httpMethod := http.MethodGet 35 odataFilterInputs := apim.OdataParameters{Filter: config.Filter, Search: config.Search, 36 Top: config.Top, Skip: config.Skip, Orderby: config.Orderby, 37 Select: config.Select, Expand: config.Expand} 38 odataFilters, urlErr := apim.OdataUtils.MakeOdataQuery(&odataFilterInputs) 39 if urlErr != nil { 40 return errors.Wrap(urlErr, "failed to create odata filter") 41 } 42 getApiProviderListURL := fmt.Sprintf("%s/apiportal/api/1.0/Management.svc/APIProviders%s", apistruct.Host, odataFilters) 43 header := make(http.Header) 44 header.Add("Accept", "application/json") 45 apiProviderListResp, httpErr := httpClient.SendRequest(httpMethod, getApiProviderListURL, nil, header, nil) 46 failureMessage := "Failed to get List of API Providers" 47 successMessage := "Successfully retrieved the api provider list from API Portal" 48 httpGetRequestParameters := cpi.HttpFileUploadRequestParameters{ 49 ErrMessage: failureMessage, 50 Response: apiProviderListResp, 51 HTTPMethod: httpMethod, 52 HTTPURL: getApiProviderListURL, 53 HTTPErr: httpErr, 54 SuccessMessage: successMessage} 55 resp, err := cpi.HTTPUploadUtils.HandleHTTPGetRequestResponse(httpGetRequestParameters) 56 commonPipelineEnvironment.custom.APIProviderList = resp 57 return err 58 }