/
filestore_utils.go
executable file
·115 lines (97 loc) · 3.2 KB
/
filestore_utils.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
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright 2024 Google LLC. All Rights Reserved.
//
// 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 filestore contains the DCL implementation of GCP Filestore.
package alpha
import (
"bytes"
"context"
"fmt"
"github.com/GoogleCloudPlatform/declarative-resource-client-library/dcl"
"github.com/GoogleCloudPlatform/declarative-resource-client-library/dcl/operations"
)
func (op *createInstanceOperation) do(ctx context.Context, r *Instance, c *Client) error {
if len(r.FileShares) != 1 {
return fmt.Errorf("Exactly one fileshare must be specified")
}
// Certain NFSExportOptions admin rules can only be set on update.
// We'll create the instance with no options and then update it to include the options.
newR := dcl.Copy(*r).(Instance)
newR.FileShares[0].NfsExportOptions = []InstanceFileSharesNfsExportOptions{}
err := createInstance(ctx, &newR, c)
if err != nil {
return err
}
c.Config.Logger.Infof("Attempting to update %v", r)
u, err := r.updateURL(c.Config.BasePath, "UpdateInstance")
if err != nil {
return err
}
u, err = dcl.AddQueryParams(u, map[string]string{"updateMask": "fileShares"})
if err != nil {
return err
}
req, err := newUpdateInstanceUpdateInstanceRequest(ctx, r, c)
if err != nil {
return err
}
c.Config.Logger.Infof("Created update: %#v", req)
body, err := marshalUpdateInstanceUpdateInstanceRequest(c, req)
if err != nil {
return err
}
resp, err := dcl.SendRequest(ctx, c.Config, "PATCH", u, bytes.NewBuffer(body), c.Config.RetryProvider)
if err != nil {
return err
}
var o operations.StandardGCPOperation
if err := dcl.ParseResponse(resp.Response, &o); err != nil {
return err
}
err = o.Wait(ctx, c.Config, "https://file.googleapis.com/v1/", "GET")
if err != nil {
return err
}
op.response, _ = o.FirstResponse()
return nil
}
func createInstance(ctx context.Context, r *Instance, c *Client) error {
c.Config.Logger.Infof("Attempting to create %v", r)
u, err := r.createURL(c.Config.BasePath)
if err != nil {
return err
}
req, err := r.marshal(c)
if err != nil {
return err
}
resp, err := dcl.SendRequest(ctx, c.Config, "POST", u, bytes.NewBuffer(req), c.Config.RetryProvider)
if err != nil {
return err
}
// wait for object to be created.
var o operations.StandardGCPOperation
if err := dcl.ParseResponse(resp.Response, &o); err != nil {
return err
}
if err := o.Wait(ctx, c.Config, "https://file.googleapis.com/v1/", "GET"); err != nil {
c.Config.Logger.Warningf("Creation failed after waiting for operation: %v", err)
return err
}
c.Config.Logger.Infof("Successfully waited for operation")
if _, err := c.GetInstance(ctx, r); err != nil {
c.Config.Logger.Warningf("get returned error: %v", err)
return err
}
return nil
}