/
vsphere.go
70 lines (56 loc) · 1.99 KB
/
vsphere.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright © 2019 Banzai Cloud
//
// 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 workflow
import (
"context"
"net/url"
"emperror.dev/errors"
"github.com/vmware/govmomi"
"github.com/vmware/govmomi/vim25/soap"
"github.com/banzaicloud/pipeline/internal/providers/pke/pkeworkflow"
"github.com/banzaicloud/pipeline/internal/providers/vsphere/pke"
"github.com/banzaicloud/pipeline/internal/secret/secrettype"
)
type VMOMIClientFactory struct {
secretStore pkeworkflow.SecretStore
}
func NewVMOMIClientFactory(secretStore pkeworkflow.SecretStore) *VMOMIClientFactory {
return &VMOMIClientFactory{secretStore: secretStore}
}
func (f *VMOMIClientFactory) New(organizationID uint, secretID string) (*govmomi.Client, error) {
s, err := f.secretStore.GetSecret(organizationID, secretID)
if err != nil {
return nil, errors.WrapIf(err, "failed to get secret")
}
if err := s.ValidateSecretType(secrettype.Vsphere); err != nil {
return nil, err
}
values := s.GetValues()
u, err := soap.ParseURL(values[secrettype.VsphereURL])
if err != nil {
return nil, err
}
u.User = url.UserPassword(values[secrettype.VsphereUser], values[secrettype.VspherePassword])
ctx := context.TODO()
// Connect and log in to ESX or vCenter
return govmomi.NewClient(ctx, u, true)
}
func getClusterTags(clusterName, nodePoolName string) map[string]string {
tags := pke.PipelineTags()
tags["banzaicloud-cluster"] = clusterName
if nodePoolName != "" {
tags["banzaicloud-nodepool"] = nodePoolName
}
return tags
}