Skip to content

Commit 303577d

Browse files
authored
feat: support policy resource (#41)
* feat: support policy data source * chore: support CRUD policy resource
1 parent 09a5119 commit 303577d

File tree

11 files changed

+810
-226
lines changed

11 files changed

+810
-226
lines changed

api/client.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,8 @@ type Client interface {
5353
ListPolicies(ctx context.Context, find *PolicyFindMessage) (*ListPolicyMessage, error)
5454
// GetPolicy gets a policy in a specific resource.
5555
GetPolicy(ctx context.Context, find *PolicyFindMessage) (*PolicyMessage, error)
56+
// UpsertPolicy creates or updates the policy.
57+
UpsertPolicy(ctx context.Context, find *PolicyFindMessage, patch *PolicyPatchMessage) (*PolicyMessage, error)
58+
// DeletePolicy deletes the policy.
59+
DeletePolicy(ctx context.Context, find *PolicyFindMessage) error
5660
}

api/policy.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,19 @@ type PolicyMessage struct {
141141
SQLReviewPolicy *SQLReviewPolicy `json:"sqlReviewPolicy"`
142142
}
143143

144+
// PolicyPatchMessage is the API message to patch the policy.
145+
type PolicyPatchMessage struct {
146+
InheritFromParent *bool `json:"inheritFromParent"`
147+
Type PolicyType `json:"type"`
148+
149+
// The policy payload
150+
DeploymentApprovalPolicy *DeploymentApprovalPolicy `json:"deploymentApprovalPolicy"`
151+
BackupPlanPolicy *BackupPlanPolicy `json:"backupPlanPolicy"`
152+
SensitiveDataPolicy *SensitiveDataPolicy `json:"sensitiveDataPolicy"`
153+
AccessControlPolicy *AccessControlPolicy `json:"accessControlPolicy"`
154+
SQLReviewPolicy *SQLReviewPolicy `json:"sqlReviewPolicy"`
155+
}
156+
144157
// ListPolicyMessage is the API message for list policy response.
145158
type ListPolicyMessage struct {
146159
Policies []*PolicyMessage `json:"policies"`

client/policy.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,65 @@ func (c *client) GetPolicy(ctx context.Context, find *api.PolicyFindMessage) (*a
6262
return &res, nil
6363
}
6464

65+
// UpsertPolicy creates or updates the policy.
66+
func (c *client) UpsertPolicy(ctx context.Context, find *api.PolicyFindMessage, patch *api.PolicyPatchMessage) (*api.PolicyMessage, error) {
67+
if find.Type == nil {
68+
return nil, errors.Errorf("invalid request, get policy must specific the policy type")
69+
}
70+
71+
payload, err := json.Marshal(patch)
72+
if err != nil {
73+
return nil, err
74+
}
75+
76+
paths := []string{}
77+
if patch.InheritFromParent != nil {
78+
paths = append(paths, "policy.inherit_from_parent")
79+
}
80+
if patch.DeploymentApprovalPolicy != nil ||
81+
patch.BackupPlanPolicy != nil ||
82+
patch.SensitiveDataPolicy != nil ||
83+
patch.AccessControlPolicy != nil ||
84+
patch.SQLReviewPolicy != nil {
85+
paths = append(paths, "policy.payload")
86+
}
87+
88+
req, err := http.NewRequestWithContext(ctx, "PATCH", fmt.Sprintf("%s/%s?allow_missing=true&update_mask=%s", c.HostURL, getPolicyRequestName(find), strings.Join(paths, ",")), strings.NewReader(string(payload)))
89+
if err != nil {
90+
return nil, err
91+
}
92+
93+
body, err := c.doRequest(req)
94+
if err != nil {
95+
return nil, err
96+
}
97+
98+
var res api.PolicyMessage
99+
err = json.Unmarshal(body, &res)
100+
if err != nil {
101+
return nil, err
102+
}
103+
104+
return &res, nil
105+
}
106+
107+
// DeletePolicy deletes the policy.
108+
func (c *client) DeletePolicy(ctx context.Context, find *api.PolicyFindMessage) error {
109+
if find.Type == nil {
110+
return errors.Errorf("invalid request, get policy must specific the policy type")
111+
}
112+
113+
req, err := http.NewRequestWithContext(ctx, "DELETE", fmt.Sprintf("%s/%s", c.HostURL, getPolicyRequestName(find)), nil)
114+
if err != nil {
115+
return err
116+
}
117+
118+
if _, err := c.doRequest(req); err != nil {
119+
return err
120+
}
121+
return nil
122+
}
123+
65124
func getPolicyRequestName(find *api.PolicyFindMessage) string {
66125
paths := []string{}
67126
if v := find.ProjectID; v != nil {

examples/setup/main.tf

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,35 @@ resource "bytebase_instance_role" "test" {
108108
bypass_rls = true
109109
}
110110
}
111+
112+
# Create deployment approval policy for test env.
113+
resource "bytebase_policy" "deployment_approval" {
114+
environment = bytebase_instance.test.environment
115+
type = "DEPLOYMENT_APPROVAL"
116+
117+
deployment_approval_policy {
118+
default_strategy = "AUTOMATIC"
119+
120+
deployment_approval_strategies {
121+
approval_group = "APPROVAL_GROUP_DBA"
122+
approval_strategy = "AUTOMATIC"
123+
deployment_type = "DATABASE_CREATE"
124+
}
125+
deployment_approval_strategies {
126+
approval_group = "APPROVAL_GROUP_PROJECT_OWNER"
127+
approval_strategy = "AUTOMATIC"
128+
deployment_type = "DATABASE_DDL"
129+
}
130+
}
131+
}
132+
133+
# Create backup plan policy for test env.
134+
resource "bytebase_policy" "backup_plan" {
135+
environment = bytebase_instance.test.environment
136+
type = "BACKUP_PLAN"
137+
138+
backup_plan_policy {
139+
schedule = "WEEKLY"
140+
retention_duration = 86400
141+
}
142+
}

0 commit comments

Comments
 (0)