add error handling for BaseUrl function

This commit is contained in:
Lukasz Zajaczkowski 2022-09-07 08:33:01 +02:00
parent 79b7fe93d8
commit bef67ee416
2 changed files with 19 additions and 22 deletions

View File

@ -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) {

View File

@ -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