-
Notifications
You must be signed in to change notification settings - Fork 24
/
workspace_controller_remote_state_sharing.go
96 lines (76 loc) · 2.67 KB
/
workspace_controller_remote_state_sharing.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package controllers
import (
"context"
"errors"
tfc "github.com/hashicorp/go-tfe"
appv1alpha2 "github.com/hashicorp/terraform-cloud-operator/api/v1alpha2"
corev1 "k8s.io/api/core/v1"
)
func (r *WorkspaceReconciler) getWorkspaces(ctx context.Context, w *workspaceInstance) (map[string]string, error) {
ws, err := w.tfClient.Client.Workspaces.List(ctx, w.instance.Spec.Organization, &tfc.WorkspaceListOptions{})
if err != nil {
return map[string]string{}, err
}
o := make(map[string]string)
for _, w := range ws.Items {
o[w.ID] = w.ID
o[w.Name] = w.ID
}
return o, nil
}
// nameOrID returns Name or ID from the passed structure where only one of them is set
func nameOrID(instanceWorkspace *appv1alpha2.ConsumerWorkspace) string {
if instanceWorkspace.Name != "" {
return instanceWorkspace.Name
}
return instanceWorkspace.ID
}
func getWorkspaceID(workspaces map[string]string, instanceWorkspace *appv1alpha2.ConsumerWorkspace) (string, error) {
k := nameOrID(instanceWorkspace)
if v, ok := workspaces[k]; ok {
return v, nil
}
return "", errors.New("workspace ID not found")
}
func (r *WorkspaceReconciler) getInstanceRemoteStateSharing(ctx context.Context, w *workspaceInstance) ([]*tfc.Workspace, error) {
iw := []*tfc.Workspace{}
if len(w.instance.Spec.RemoteStateSharing.Workspaces) == 0 {
return iw, nil
}
workspaces, err := r.getWorkspaces(ctx, w)
if err != nil {
return iw, err
}
for _, workspace := range w.instance.Spec.RemoteStateSharing.Workspaces {
wID, err := getWorkspaceID(workspaces, workspace)
if err != nil {
w.log.Error(err, "Reconcile Remote State Sharing", "msg", "failed to get workspace ID")
r.Recorder.Event(&w.instance, corev1.EventTypeWarning, "ReconcileRemoteStateSharing", "Failed to get workspace ID")
return iw, err
}
iw = append(iw, &tfc.Workspace{ID: wID})
}
return iw, nil
}
func (r *WorkspaceReconciler) reconcileRemoteStateSharing(ctx context.Context, w *workspaceInstance) error {
w.log.Info("Reconcile Remote State Sharing", "msg", "new reconciliation event")
if w.instance.Spec.RemoteStateSharing == nil {
return nil
}
instanceRemoteStateSharing, err := r.getInstanceRemoteStateSharing(ctx, w)
if err != nil {
w.log.Error(err, "Reconcile Remote State Sharing", "msg", "failed to get instance remote state sharing workspace sources")
return err
}
if len(instanceRemoteStateSharing) > 0 {
err = w.tfClient.Client.Workspaces.UpdateRemoteStateConsumers(ctx, w.instance.Status.WorkspaceID, tfc.WorkspaceUpdateRemoteStateConsumersOptions{
Workspaces: instanceRemoteStateSharing,
})
if err != nil {
return err
}
}
return nil
}