This repository has been archived by the owner on Dec 12, 2023. It is now read-only.
/
resource_client.go
103 lines (90 loc) · 2.73 KB
/
resource_client.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright © 2019 Bitgrip <berlin@bitgrip.de>
//
// 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 client
import (
"fmt"
"github.com/sirupsen/logrus"
)
// EmptyResourceClient is a ResourceClient with always exists and dose nothing
var EmptyResourceClient = emptyResourceClient{}
type resourceClient struct {
id string
name string
logger *logrus.Entry
}
func (client *resourceClient) ID() (string, error) {
return client.id, nil
}
func (client *resourceClient) Name() (string, error) {
return client.name, nil
}
func (client *resourceClient) Exists() (bool, error) {
return false, fmt.Errorf("Exists not supported")
}
func (client *resourceClient) Create() error {
return fmt.Errorf("Create not supported")
}
func (client *resourceClient) Upgrade() error {
return fmt.Errorf("Upgrade not supported")
}
func (client *resourceClient) Delete() error {
return fmt.Errorf("Delete not supported")
}
type namespacedResourceClient struct {
resourceClient
namespaceID string
namespace string
project ProjectClient
}
func (client *namespacedResourceClient) NamespaceID() (string, error) {
if client.namespace == "" {
return "", nil
}
if client.namespaceID != "" {
return client.namespaceID, nil
}
var namespace NamespaceClient
var err error
if namespace, err = client.project.Namespace(client.namespace); err != nil {
client.logger.WithError(err).Error("Failed to read namespaceID")
return "", fmt.Errorf("Failed to read namespaceID, %v", err)
}
if client.namespaceID, err = namespace.ID(); err != nil {
client.logger.WithError(err).Error("Failed to read namespaceID")
return "", fmt.Errorf("Failed to read namespaceID, %v", err)
}
return client.namespaceID, nil
}
func (client *namespacedResourceClient) Namespace() (string, error) {
return client.namespace, nil
}
type emptyResourceClient struct{}
func (client emptyResourceClient) ID() (string, error) {
return "", nil
}
func (client emptyResourceClient) Name() (string, error) {
return "", nil
}
func (client emptyResourceClient) Exists() (bool, error) {
return true, nil
}
func (client emptyResourceClient) Create() error {
return nil
}
func (client emptyResourceClient) Upgrade() error {
return nil
}
func (client emptyResourceClient) Delete() error {
return nil
}