github.com/jaylevin/jenkins-library@v1.230.4/cmd/apiProviderDownload.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"net/http"
     7  	"os"
     8  
     9  	"github.com/SAP/jenkins-library/pkg/command"
    10  	"github.com/SAP/jenkins-library/pkg/cpi"
    11  	piperhttp "github.com/SAP/jenkins-library/pkg/http"
    12  	"github.com/SAP/jenkins-library/pkg/log"
    13  	"github.com/SAP/jenkins-library/pkg/piperutils"
    14  	"github.com/SAP/jenkins-library/pkg/telemetry"
    15  	"github.com/pkg/errors"
    16  )
    17  
    18  type apiProviderDownloadUtils interface {
    19  	command.ExecRunner
    20  	FileWrite(path string, content []byte, perm os.FileMode) error
    21  	FileExists(filename string) (bool, error)
    22  }
    23  
    24  type apiProviderDownloadUtilsBundle struct {
    25  	*command.Command
    26  	*piperutils.Files
    27  }
    28  
    29  func newApiProviderDownloadUtils() apiProviderDownloadUtils {
    30  	utils := apiProviderDownloadUtilsBundle{
    31  		Command: &command.Command{},
    32  		Files:   &piperutils.Files{},
    33  	}
    34  	utils.Stdout(log.Writer())
    35  	utils.Stderr(log.Writer())
    36  	return &utils
    37  }
    38  
    39  func apiProviderDownload(config apiProviderDownloadOptions, telemetryData *telemetry.CustomData) {
    40  	utils := newApiProviderDownloadUtils()
    41  	httpClient := &piperhttp.Client{}
    42  	err := runApiProviderDownload(&config, telemetryData, httpClient, utils)
    43  	if err != nil {
    44  		log.Entry().WithError(err).Fatal("step execution failed")
    45  	}
    46  }
    47  
    48  func runApiProviderDownload(config *apiProviderDownloadOptions, telemetryData *telemetry.CustomData, httpClient piperhttp.Sender, utils apiProviderDownloadUtils) error {
    49  	clientOptions := piperhttp.ClientOptions{}
    50  	header := make(http.Header)
    51  	header.Add("Accept", "application/json")
    52  	serviceKey, err := cpi.ReadCpiServiceKey(config.APIServiceKey)
    53  	if err != nil {
    54  		return err
    55  	}
    56  	downloadArtifactURL := fmt.Sprintf("%s/apiportal/api/1.0/Management.svc/APIProviders('%s')", serviceKey.OAuth.Host, config.APIProviderName)
    57  	tokenParameters := cpi.TokenParameters{TokenURL: serviceKey.OAuth.OAuthTokenProviderURL,
    58  		Username: serviceKey.OAuth.ClientID, Password: serviceKey.OAuth.ClientSecret, Client: httpClient}
    59  	token, err := cpi.CommonUtils.GetBearerToken(tokenParameters)
    60  	if err != nil {
    61  		return errors.Wrap(err, "failed to fetch Bearer Token")
    62  	}
    63  	clientOptions.Token = fmt.Sprintf("Bearer %s", token)
    64  	httpClient.SetOptions(clientOptions)
    65  	httpMethod := http.MethodGet
    66  	downloadResp, httpErr := httpClient.SendRequest(httpMethod, downloadArtifactURL, nil, header, nil)
    67  	if httpErr != nil {
    68  		return errors.Wrapf(httpErr, "HTTP %v request to %v failed with error", httpMethod, downloadArtifactURL)
    69  	}
    70  	if downloadResp == nil {
    71  		return errors.Errorf("did not retrieve a HTTP response: %v", httpErr)
    72  	}
    73  	if downloadResp != nil && downloadResp.Body != nil {
    74  		defer downloadResp.Body.Close()
    75  	}
    76  	if downloadResp.StatusCode == 200 {
    77  		jsonFilePath := config.DownloadPath
    78  		content, err := ioutil.ReadAll(downloadResp.Body)
    79  		if err != nil {
    80  			return err
    81  		}
    82  		err = utils.FileWrite(jsonFilePath, content, 0775)
    83  		if err != nil {
    84  			return err
    85  		}
    86  	}
    87  	return nil
    88  }