@@ -7,19 +7,12 @@ import (
77 "net/http"
88 "strings"
99
10- "github.com/google/go-querystring/query"
11-
1210 "github.com/bytebase/terraform-provider-bytebase/api"
1311)
1412
15- // ListInstance will return all instances.
16- func (c * client ) ListInstance (ctx context.Context , find * api.InstanceFind ) ([]* api.Instance , error ) {
17- q , err := query .Values (find )
18- if err != nil {
19- return nil , err
20- }
21-
22- req , err := http .NewRequestWithContext (ctx , "GET" , fmt .Sprintf ("%s/instance?%s" , c .HostURL , q .Encode ()), nil )
13+ // ListInstance will return instances in environment.
14+ func (c * client ) ListInstance (ctx context.Context , find * api.InstanceFindMessage ) (* api.ListInstanceMessage , error ) {
15+ req , err := http .NewRequestWithContext (ctx , "GET" , fmt .Sprintf ("%s/environments/%s/instances?showDeleted=%v" , c .HostURL , find .EnvironmentID , find .ShowDeleted ), nil )
2316 if err != nil {
2417 return nil , err
2518 }
@@ -29,44 +22,45 @@ func (c *client) ListInstance(ctx context.Context, find *api.InstanceFind) ([]*a
2922 return nil , err
3023 }
3124
32- orders := [] * api.Instance {}
33- err = json .Unmarshal (body , & orders )
25+ var res api.ListInstanceMessage
26+ err = json .Unmarshal (body , & res )
3427 if err != nil {
3528 return nil , err
3629 }
3730
38- return orders , nil
31+ return & res , nil
3932}
4033
41- // CreateInstance creates the instance.
42- func (c * client ) CreateInstance (ctx context.Context , create * api.InstanceCreate ) (* api.Instance , error ) {
43- payload , err := json . Marshal ( create )
34+ // GetInstance gets the instance by id .
35+ func (c * client ) GetInstance (ctx context.Context , find * api.InstanceFindMessage ) (* api.InstanceMessage , error ) {
36+ req , err := http . NewRequestWithContext ( ctx , "GET" , fmt . Sprintf ( "%s/environments/%s/instances/%s?showDeleted=%v" , c . HostURL , find . EnvironmentID , find . InstanceID , find . ShowDeleted ), nil )
4437 if err != nil {
4538 return nil , err
4639 }
4740
48- req , err := http . NewRequestWithContext ( ctx , "POST" , fmt . Sprintf ( "%s/instance" , c . HostURL ), strings . NewReader ( string ( payload )) )
41+ body , err := c . doRequest ( req )
4942 if err != nil {
5043 return nil , err
5144 }
5245
53- body , err := c .doRequest (req )
46+ var res api.InstanceMessage
47+ err = json .Unmarshal (body , & res )
5448 if err != nil {
5549 return nil , err
5650 }
5751
58- var instance api.Instance
59- err = json .Unmarshal (body , & instance )
52+ return & res , nil
53+ }
54+
55+ // CreateInstance creates the instance.
56+ func (c * client ) CreateInstance (ctx context.Context , environmentID , instanceID string , instance * api.InstanceMessage ) (* api.InstanceMessage , error ) {
57+ payload , err := json .Marshal (instance )
6058 if err != nil {
6159 return nil , err
6260 }
6361
64- return & instance , nil
65- }
62+ req , err := http .NewRequestWithContext (ctx , "POST" , fmt .Sprintf ("%s/environments/%s/instances?instanceId=%s" , c .HostURL , environmentID , instanceID ), strings .NewReader (string (payload )))
6663
67- // GetInstance gets the instance by id.
68- func (c * client ) GetInstance (ctx context.Context , instanceID int ) (* api.Instance , error ) {
69- req , err := http .NewRequestWithContext (ctx , "GET" , fmt .Sprintf ("%s/instance/%d" , c .HostURL , instanceID ), nil )
7064 if err != nil {
7165 return nil , err
7266 }
@@ -76,23 +70,24 @@ func (c *client) GetInstance(ctx context.Context, instanceID int) (*api.Instance
7670 return nil , err
7771 }
7872
79- var instance api.Instance
80- err = json .Unmarshal (body , & instance )
73+ var res api.InstanceMessage
74+ err = json .Unmarshal (body , & res )
8175 if err != nil {
8276 return nil , err
8377 }
8478
85- return & instance , nil
79+ return & res , nil
8680}
8781
8882// UpdateInstance updates the instance.
89- func (c * client ) UpdateInstance (ctx context.Context , instanceID int , patch * api.InstancePatch ) (* api.Instance , error ) {
90- payload , err := json .Marshal (patch )
83+ func (c * client ) UpdateInstance (ctx context.Context , environmentID , instanceID string , instance * api.InstanceMessage ) (* api.InstanceMessage , error ) {
84+ payload , err := json .Marshal (instance )
9185 if err != nil {
9286 return nil , err
9387 }
9488
95- req , err := http .NewRequestWithContext (ctx , "PATCH" , fmt .Sprintf ("%s/instance/%d" , c .HostURL , instanceID ), strings .NewReader (string (payload )))
89+ req , err := http .NewRequestWithContext (ctx , "PATCH" , fmt .Sprintf ("%s/environments/%s/instances/%s" , c .HostURL , environmentID , instanceID ), strings .NewReader (string (payload )))
90+
9691 if err != nil {
9792 return nil , err
9893 }
@@ -102,18 +97,18 @@ func (c *client) UpdateInstance(ctx context.Context, instanceID int, patch *api.
10297 return nil , err
10398 }
10499
105- var instance api.Instance
106- err = json .Unmarshal (body , & instance )
100+ var res api.InstanceMessage
101+ err = json .Unmarshal (body , & res )
107102 if err != nil {
108103 return nil , err
109104 }
110105
111- return & instance , nil
106+ return & res , nil
112107}
113108
114109// DeleteInstance deletes the instance.
115- func (c * client ) DeleteInstance (ctx context.Context , instanceID int ) error {
116- req , err := http .NewRequestWithContext (ctx , "DELETE" , fmt .Sprintf ("%s/instance/%d " , c .HostURL , instanceID ), nil )
110+ func (c * client ) DeleteInstance (ctx context.Context , environmentID , instanceID string ) error {
111+ req , err := http .NewRequestWithContext (ctx , "DELETE" , fmt .Sprintf ("%s/environments/%s/instances/%s " , c .HostURL , environmentID , instanceID ), nil )
117112 if err != nil {
118113 return err
119114 }
0 commit comments