From bef67ee41666964870d81979eaad982ee4452229 Mon Sep 17 00:00:00 2001 From: Lukasz Zajaczkowski Date: Wed, 7 Sep 2022 08:33:01 +0200 Subject: [PATCH] add error handling for BaseUrl function --- provider/plural/client.go | 33 ++++++++++++--------------------- provider/plural/plural.go | 8 +++++++- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/provider/plural/client.go b/provider/plural/client.go index ed84897cb..ecb83dcc1 100644 --- a/provider/plural/client.go +++ b/provider/plural/client.go @@ -1,25 +1,10 @@ -/* -Copyright 2022 The Kubernetes Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - package plural import ( "context" "fmt" "net/http" + "net/url" "strings" "github.com/pluralsh/gqlclient" @@ -61,8 +46,11 @@ type DnsRecord struct { Records []string } -func NewClient(conf *Config) Client { - base := conf.BaseUrl() +func NewClient(conf *Config) (Client, error) { + base, err := conf.BaseUrl() + if err != nil { + return nil, err + } httpClient := http.Client{ Transport: &authedTransport{ key: conf.Token, @@ -74,15 +62,18 @@ func NewClient(conf *Config) Client { ctx: context.Background(), pluralClient: gqlclient.NewClient(&httpClient, endpoint), config: conf, - } + }, nil } -func (c *Config) BaseUrl() string { +func (c *Config) BaseUrl() (string, error) { host := "https://app.plural.sh" if c.Endpoint != "" { host = fmt.Sprintf("https://%s", c.Endpoint) + if _, err := url.Parse(host); err != nil { + return "", err + } } - return host + return host, nil } func (client *client) DnsRecords() ([]*DnsRecord, error) { diff --git a/provider/plural/plural.go b/provider/plural/plural.go index 8fb2ca589..71934f430 100644 --- a/provider/plural/plural.go +++ b/provider/plural/plural.go @@ -56,8 +56,14 @@ func NewPluralProvider(cluster, provider string) (*PluralProvider, error) { Cluster: cluster, Provider: provider, } + + client, err := NewClient(config) + if err != nil { + return nil, err + } + prov := &PluralProvider{ - Client: NewClient(config), + Client: client, } return prov, nil