From 579d1391aab54481517c3c0b705eb9ea8b21f27e Mon Sep 17 00:00:00 2001 From: Simon Bouchard Date: Tue, 31 Oct 2023 18:25:59 -0400 Subject: [PATCH 1/2] Add another method to create a REST Client by allowing a http.client paramater to be passed. Allowing CA/certs to be configured or other TLS settings --- apiv2/client.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/apiv2/client.go b/apiv2/client.go index 9cb65f25..b08da58e 100644 --- a/apiv2/client.go +++ b/apiv2/client.go @@ -2,6 +2,7 @@ package apiv2 import ( "context" + "net/http" "net/url" "strings" @@ -150,6 +151,24 @@ func NewRESTClientForHost(u, username, password string, opts *config.Options) (* return NewRESTClient(v2SwaggerClient, opts, authInfo), nil } +// NewRESTClientForHostWithClient constructs a new REST client containing a swagger API client using the defined +// host string and basePath, the additional Harbor v2 API suffix as well as basic auth info while using provided http client. +func NewRESTClientForHostWithClient(u, username, password string, opts *config.Options, client *http.Client) (*RESTClient, error) { + if !strings.HasSuffix(u, v2URLSuffix) { + u += v2URLSuffix + } + + harborURL, err := url.Parse(u) + if err != nil { + return nil, err + } + + v2SwaggerClient := v2client.New(runtimeclient.NewWithClient(harborURL.Host, harborURL.Path, []string{harborURL.Scheme}, client), strfmt.Default) + authInfo := runtimeclient.BasicAuth(username, password) + + return NewRESTClient(v2SwaggerClient, opts, authInfo), nil +} + // NewRESTClientWithAuthFunc constructs a new REST client containing a swagger API client using the defined // host string and basePath, the additional Harbor v2 API suffix as well as a custom auth func, e.g. basic auth or token auth. func NewRESTClientWithAuthFunc(u string, authFunc runtime.ClientAuthInfoWriterFunc, opts *config.Options) (*RESTClient, error) { From 8acc785a4693c8b5be324208abcabd8ec7fc9d48 Mon Sep 17 00:00:00 2001 From: Simon Bouchard Date: Mon, 6 Nov 2023 17:49:33 -0500 Subject: [PATCH 2/2] Added example on how to use new REST client with http.client --- apiv2/client_test.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/apiv2/client_test.go b/apiv2/client_test.go index cca94af9..ff45785a 100644 --- a/apiv2/client_test.go +++ b/apiv2/client_test.go @@ -71,6 +71,42 @@ func ExampleNewRESTClient() { } } +func ExampleNewRESTClientWithHttpClient() { + // This example constructs a new (goharbor) REST client + // and create an example project. + ctx := context.Background() + apiURL := "harbor.mydomain.com/api" + username := "user" + password := "password" + + var optsTLS runtimeclient.TLSClientOptions + // optsTLS.Certificate = "/path/to/client.crt" + // optsTLS.Key = "/path/to/client.Key" + // optsTLS.CA = "/path/to/rootca.cert.pem" + client, err := runtimeclient.TLSClient(optsTLS) + if err != nil { + return nil, err + } + + harborURL, err := url.Parse(apiURL) + if err != nil { + panic(err) + } + + v2SwaggerClient := v2client.New(runtimeclient.NewWithClient(harborURL.Host, harborURL.Path, []string{harborURL.Scheme}), strfmt.Default) + authInfo := runtimeclient.BasicAuth(username, password) + + harborClient := NewRESTClient(v2SwaggerClient, nil, authInfo) + + err = harborClient.NewProject(ctx, &model.ProjectReq{ + ProjectName: "my-project", + }) + + if err != nil { + panic(err) + } +} + func ExampleNewRESTClient_withOptions() { // This example constructs a new (goharbor) REST client using the provided 'options', // and lists all projects matching the 'options' configuration.