Skip to content

Commit fd4ce5e

Browse files
committed
Merge branch 'main' into feature/network-tags
2 parents 5c7af87 + 61b6aa5 commit fd4ce5e

File tree

7 files changed

+84
-342
lines changed

7 files changed

+84
-342
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
bin
88
testbin/*
99

10+
pkg/mocks/*
11+
!pkg/mocks/.keep
12+
1013
# Various build/test artifacts.
1114
.tiltbuild
1215
*log

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ api/%/zz_generated.deepcopy.go: bin/controller-gen $(DEEPCOPY_GEN_INPUTS)
7474

7575
MANAGER_BIN_INPUTS=$(shell find ./controllers ./api ./pkg -name "*mock*" -prune -o -name "*test*" -prune -o -type f -print) main.go go.mod go.sum
7676
.PHONY: build
77-
build: binaries generate-deepcopy manifests release-manifests bin/manager bin/mockgen ## Build manager binary.
77+
build: binaries generate-mocks generate-deepcopy manifests release-manifests ## Build manager binary.
7878
bin/manager: $(MANAGER_BIN_INPUTS)
7979
go fmt ./...
8080
go vet ./...
@@ -170,7 +170,7 @@ test: generate-mocks lint generate-deepcopy bin/ginkgo bin/kubectl bin/kube-apis
170170
./hack/testing_ginkgo_recover_statements.sh --remove; exit $$EXIT_STATUS
171171

172172
.PHONY: generate-mocks
173-
generate-mocks: bin/mockgen $(shell find ./pkg/mocks -type f -name "mock*.go") ## Generate mocks needed for testing. Primarily mocks of the cloud package.
173+
generate-mocks: bin/mockgen pkg/mocks/mock_client.go $(shell find ./pkg/mocks -type f -name "mock*.go") ## Generate mocks needed for testing. Primarily mocks of the cloud package.
174174
pkg/mocks/mock%.go: $(shell find ./pkg/cloud -type f -name "*test*" -prune -o -print)
175175
go generate ./...
176176

pkg/cloud/client.go

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,37 +46,36 @@ type client struct {
4646
// csA *cloudstack.CloudStackClient
4747
}
4848

49+
// cloud-config ini structure.
50+
type config struct {
51+
ApiUrl string `ini:"api-url"`
52+
ApiKey string `ini:"api-key"`
53+
SecretKey string `ini:"secret-key"`
54+
VerifySSL bool `ini:"verify-ssl"`
55+
}
56+
4957
func NewClient(cc_path string) (Client, error) {
5058
c := &client{}
51-
apiUrl, apiKey, secretKey, err := readAPIConfig(cc_path)
52-
if err != nil {
59+
cfg := &config{VerifySSL: true}
60+
if rawCfg, err := ini.Load(cc_path); err != nil {
5361
return nil, errors.Wrapf(err, "Error encountered while reading config at path: %s", cc_path)
62+
} else if g := rawCfg.Section("Global"); len(g.Keys()) == 0 {
63+
return nil, errors.New("Section Global not found.")
64+
} else if err = rawCfg.Section("Global").StrictMapTo(cfg); err != nil {
65+
return nil, errors.Wrapf(err, "Error encountered while parsing [Global] section from config at path: %s", cc_path)
5466
}
5567

5668
// This is a placeholder for sending non-blocking requests.
5769
// c.csA = cloudstack.NewClient(apiUrl, apiKey, secretKey, false)
5870
// TODO: attempt a less clunky client liveliness check (not just listing zones).
59-
c.cs = cloudstack.NewAsyncClient(apiUrl, apiKey, secretKey, false)
60-
_, err = c.cs.Zone.ListZones(c.cs.Zone.NewListZonesParams())
71+
c.cs = cloudstack.NewAsyncClient(cfg.ApiUrl, cfg.ApiKey, cfg.SecretKey, cfg.VerifySSL)
72+
_, err := c.cs.Zone.ListZones(c.cs.Zone.NewListZonesParams())
6173
if err != nil && strings.Contains(err.Error(), "i/o timeout") {
6274
return c, errors.Wrap(err, "Timeout while checking CloudStack API Client connectivity.")
6375
}
6476
return c, errors.Wrap(err, "Error encountered while checking CloudStack API Client connectivity.")
6577
}
6678

67-
// CloudStack API config reader.
68-
func readAPIConfig(cc_path string) (string, string, string, error) {
69-
cfg, err := ini.Load(cc_path)
70-
if err != nil {
71-
return "", "", "", err
72-
}
73-
g := cfg.Section("Global")
74-
if len(g.Keys()) == 0 {
75-
return "", "", "", errors.New("section Global not found")
76-
}
77-
return g.Key("api-url").Value(), g.Key("api-key").Value(), g.Key("secret-key").Value(), err
78-
}
79-
8079
func NewClientFromCSAPIClient(cs *cloudstack.CloudStackClient) Client {
8180
c := &client{cs: cs}
8281
return c

pkg/cloud/client_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
Copyright 2022.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package cloud_test
18+
19+
import (
20+
"fmt"
21+
22+
. "github.com/onsi/ginkgo"
23+
. "github.com/onsi/gomega"
24+
"gopkg.in/ini.v1"
25+
)
26+
27+
// Example cloud-config ini structure.
28+
type Global struct {
29+
ApiUrl string `ini:"api-url"`
30+
VerifySSL bool `ini:"verify-ssl"`
31+
}
32+
33+
var _ = Describe("Instance", func() {
34+
35+
var ()
36+
37+
BeforeEach(func() {
38+
// This test fixture is useful for development, but the actual method of parsing is confinded to the client's
39+
// new client method. The parsing used here is more of a schema, and we don't need to test another library's
40+
// abilities to parse said schema.
41+
Skip("Dev test suite.")
42+
})
43+
44+
AfterEach(func() {
45+
})
46+
47+
Context("When fetching an INI config.", func() {
48+
It("Handles the positive case.", func() {
49+
cfg := &Global{}
50+
rawCfg, err := ini.Load("../../cloud-config")
51+
Ω(rawCfg.Section("Global")).ShouldNot(BeNil())
52+
fmt.Println(rawCfg.Section("Global"))
53+
Ω(err).ShouldNot(HaveOccurred())
54+
Ω(rawCfg.Section("Global").MapTo(cfg)).Should(Succeed())
55+
Ω(cfg.VerifySSL).Should(BeFalse())
56+
Ω(cfg.ApiUrl).ShouldNot(BeEmpty())
57+
})
58+
})
59+
})

pkg/cloud/helpers_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,14 @@ const (
3838

3939
var _ = Describe("Helpers", func() {
4040

41-
It("Gets API configuration", func() {
42-
Context("For a configuration with the 'Global' section missing", func() {
41+
Context("For a configuration with the 'Global' section missing", func() {
42+
It("Gets API configuration", func() {
4343
filepath := getConfigPath("cloud-config-no-global")
4444

4545
client, err := cloud.NewClient(filepath)
46+
4647
Ω(client).Should(BeNil())
47-
Ω(err.Error()).Should(ContainSubstring("section Global not found"))
48+
Ω(err.Error()).Should(ContainSubstring("Section Global not found"))
4849
})
4950
})
5051

pkg/mocks/.keep

Whitespace-only changes.

0 commit comments

Comments
 (0)