-
Notifications
You must be signed in to change notification settings - Fork 332
/
auth_policies_create.go
73 lines (64 loc) · 1.82 KB
/
auth_policies_create.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
package cmd
import (
"encoding/json"
"io"
"net/http"
"os"
"github.com/spf13/cobra"
"github.com/treeverse/lakefs/pkg/api/apigen"
)
const policyCreatedTemplate = `{{ "Policy created successfully." | green }}
` + policyDetailsTemplate
var authPoliciesCreate = &cobra.Command{
Use: "create",
Short: "Create a policy",
Run: func(cmd *cobra.Command, args []string) {
id := Must(cmd.Flags().GetString("id"))
document := Must(cmd.Flags().GetString("statement-document"))
clt := getClient()
var err error
var fp io.ReadCloser
if document == "-" {
fp = os.Stdin
} else {
fp, err = os.Open(document)
if err != nil {
DieFmt("could not open policy document: %v", err)
}
defer func() {
_ = fp.Close()
}()
}
var doc StatementDoc
err = json.NewDecoder(fp).Decode(&doc)
if err != nil {
DieFmt("could not parse statement JSON document: %v", err)
}
resp, err := clt.CreatePolicyWithResponse(cmd.Context(), apigen.CreatePolicyJSONRequestBody{
Id: id,
Statement: doc.Statement,
})
DieOnErrorOrUnexpectedStatusCode(resp, err, http.StatusCreated)
if resp.JSON201 == nil {
Die("Bad response from server", 1)
}
createdPolicy := resp.JSON201
Write(policyCreatedTemplate, struct {
ID string
CreationDate int64
StatementDoc StatementDoc
}{
ID: createdPolicy.Id,
CreationDate: *createdPolicy.CreationDate,
StatementDoc: StatementDoc{createdPolicy.Statement},
})
},
}
//nolint:gochecknoinits
func init() {
authPoliciesCreate.Flags().String("id", "", "Policy identifier")
_ = authPoliciesCreate.MarkFlagRequired("id")
authPoliciesCreate.Flags().String("statement-document", "", "JSON statement document path (or \"-\" for stdin)")
_ = authPoliciesCreate.MarkFlagRequired("statement-document")
authPoliciesCmd.AddCommand(authPoliciesCreate)
}