Skip to content

Commit fbded6f

Browse files
authored
chore: check if the parent resource for the policy exists (#48)
* feat: support policy data source * chore: support CRUD policy resource * chore: support config sql review rules * chore: update docs * chore: update version * chore: update docs * chore: update docs * chore: update docs * chore: optimize * chore: update * chore: update version * chore: add tests * chore: update access control policy & sql review policy * chore: check if the parent resource for the policy exists * chore: hide policy
1 parent 05ca1f8 commit fbded6f

File tree

19 files changed

+166
-751
lines changed

19 files changed

+166
-751
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.0.7-alpha.7
1+
0.0.7

api/client.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,12 @@ type Client interface {
5959
UpsertPolicy(ctx context.Context, find *PolicyFindMessage, patch *PolicyPatchMessage) (*PolicyMessage, error)
6060
// DeletePolicy deletes the policy.
6161
DeletePolicy(ctx context.Context, find *PolicyFindMessage) error
62+
63+
// Database
64+
// GetDatabase gets the database by environment resource id, instance resource id and the database name.
65+
GetDatabase(ctx context.Context, find *DatabaseFindMessage) (*DatabaseMessage, error)
66+
67+
// Project
68+
// GetProject gets the project by resource id.
69+
GetProject(ctx context.Context, projectID string) (*PorjectMessage, error)
6270
}

api/database.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package api
2+
3+
// DatabaseFindMessage is the API message for finding database.
4+
type DatabaseFindMessage struct {
5+
EnvironmentID string
6+
InstanceID string
7+
DatabaseName string
8+
}
9+
10+
// DatabaseMessage is the API message for database.
11+
type DatabaseMessage struct {
12+
Name string `json:"name"`
13+
Project string `json:"project"`
14+
SchemaVersion string `json:"schemaVersion"`
15+
}

api/project.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package api
2+
3+
// PorjectMessage is the API message for project.
4+
type PorjectMessage struct {
5+
Name string `json:"name"`
6+
Title string `json:"title"`
7+
Key string `json:"key"`
8+
Workflow string `json:"workflow"`
9+
Visibility string `json:"visibility"`
10+
TenantMode string `json:"tenantMode"`
11+
DBNameTemplate string `json:"dbNameTemplate"`
12+
SchemaVersion string `json:"schemaVersion"`
13+
SchemaChange string `json:"schemaChange"`
14+
LGTMCheck string `json:"lgtmCheck"`
15+
}

client/database.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package client
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
"net/http"
8+
9+
"github.com/bytebase/terraform-provider-bytebase/api"
10+
)
11+
12+
// GetDatabase gets the database by environment resource id, instance resource id and the database name.
13+
func (c *client) GetDatabase(ctx context.Context, find *api.DatabaseFindMessage) (*api.DatabaseMessage, error) {
14+
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/%s/environments/%s/instances/%s/databases/%s", c.url, c.version, find.EnvironmentID, find.InstanceID, find.DatabaseName), nil)
15+
if err != nil {
16+
return nil, err
17+
}
18+
19+
body, err := c.doRequest(req)
20+
if err != nil {
21+
return nil, err
22+
}
23+
24+
var res api.DatabaseMessage
25+
err = json.Unmarshal(body, &res)
26+
if err != nil {
27+
return nil, err
28+
}
29+
30+
return &res, nil
31+
}

client/project.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package client
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
"net/http"
8+
9+
"github.com/bytebase/terraform-provider-bytebase/api"
10+
)
11+
12+
// GetProject gets the project by resource id.
13+
func (c *client) GetProject(ctx context.Context, projectID string) (*api.PorjectMessage, error) {
14+
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/%s/projects/%s", c.url, c.version, projectID), nil)
15+
if err != nil {
16+
return nil, err
17+
}
18+
19+
body, err := c.doRequest(req)
20+
if err != nil {
21+
return nil, err
22+
}
23+
24+
var res api.PorjectMessage
25+
err = json.Unmarshal(body, &res)
26+
if err != nil {
27+
return nil, err
28+
}
29+
30+
return &res, nil
31+
}

docs/data-sources/policy.md

Lines changed: 0 additions & 51 deletions
This file was deleted.

docs/data-sources/policy_list.md

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)