Skip to content

Commit

Permalink
Support of query params for parent (#78)
Browse files Browse the repository at this point in the history
* Support of query params for Apply

* Adapt get and delete to query params

* same for gateway

* tests and fixes
  • Loading branch information
gbecan authored Jan 7, 2025
1 parent 3153ac9 commit 0d5dc6d
Show file tree
Hide file tree
Showing 10 changed files with 260 additions and 66 deletions.
58 changes: 52 additions & 6 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,52 @@ func TestApplyShouldWorkWithExternalAuthMode(t *testing.T) {
}
}

func TestApplyShouldWorkWithQueryParams(t *testing.T) {
defer httpmock.Reset()
baseUrl := "http://baseUrl"
apiKey := "aToken"
client, err := Make(ApiParameter{
ApiKey: apiKey,
BaseUrl: baseUrl,
})
if err != nil {
panic(err)
}
client.setAuthMethodFromEnvIfNeeded()
httpmock.ActivateNonDefault(
client.client.GetClient(),
)
responder := httpmock.NewStringResponder(200, `{"upsertResult": "NotChanged"}`)

topic := resource.Resource{
Json: []byte(`{"yolo": "data"}`),
Kind: "Alert",
Name: "my-alert",
Version: "v3",
Metadata: map[string]interface{}{
"appInstance": "my-app",
},
}

httpmock.RegisterMatcherResponderWithQuery(
"PUT",
"http://baseUrl/api/public/monitoring/v3/alert?appInstance=my-app",
nil,
httpmock.HeaderIs("Authorization", "Bearer "+apiKey).
And(httpmock.HeaderIs("X-CDK-CLIENT", "CLI/unknown")).
And(httpmock.BodyContainsBytes(topic.Json)),
responder,
)

body, err := client.Apply(&topic, false)
if err != nil {
t.Error(err)
}
if body != "NotChanged" {
t.Errorf("Bad result expected NotChanged got: %s", body)
}
}

func TestApplyWithDryModeShouldWork(t *testing.T) {
defer httpmock.Reset()
baseUrl := "http://baseUrl"
Expand Down Expand Up @@ -237,7 +283,7 @@ func TestGetShouldWork(t *testing.T) {
)

app := client.GetKinds()["Application"]
result, err := client.Get(&app, []string{}, nil)
result, err := client.Get(&app, []string{}, []string{}, nil)
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -274,7 +320,7 @@ func TestGetShouldFailIfN2xx(t *testing.T) {
)

app := client.GetKinds()["Application"]
_, err = client.Get(&app, []string{}, nil)
_, err = client.Get(&app, []string{}, []string{}, nil)
if err == nil {
t.Failed()
}
Expand Down Expand Up @@ -309,7 +355,7 @@ func TestDescribeShouldWork(t *testing.T) {
)

app := client.GetKinds()["Application"]
result, err := client.Describe(&app, []string{}, "yo")
result, err := client.Describe(&app, []string{}, []string{}, "yo")
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -347,7 +393,7 @@ func TestDescribeShouldFailIfNo2xx(t *testing.T) {
)

app := client.GetKinds()["Application"]
_, err = client.Describe(&app, []string{}, "yo")
_, err = client.Describe(&app, []string{}, []string{}, "yo")
if err == nil {
t.Failed()
}
Expand Down Expand Up @@ -382,7 +428,7 @@ func TestDeleteShouldWork(t *testing.T) {
)

app := client.GetKinds()["Application"]
err = client.Delete(&app, []string{}, "yo")
err = client.Delete(&app, []string{}, []string{}, "yo")
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -453,7 +499,7 @@ func TestDeleteShouldFailOnNot2XX(t *testing.T) {
)

app := client.GetKinds()["Application"]
err = client.Delete(&app, []string{}, "yo")
err = client.Delete(&app, []string{}, []string{}, "yo")
if err == nil {
t.Fail()
}
Expand Down
37 changes: 27 additions & 10 deletions client/console_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,12 +254,15 @@ func (client *Client) Apply(resource *resource.Resource, dryMode bool) (string,
if !ok {
return "", fmt.Errorf("kind %s not found", resource.Kind)
}
applyPath, err := kind.ApplyPath(resource)
applyQueryInfo, err := kind.ApplyPath(resource)
if err != nil {
return "", err
}
url := client.baseUrl + applyPath
url := client.baseUrl + applyQueryInfo.Path
builder := client.client.R().SetBody(resource.Json)
for _, param := range applyQueryInfo.QueryParams {
builder = builder.SetQueryParam(param.Name, param.Value)
}
if dryMode {
builder = builder.SetQueryParam("dryMode", "true")
}
Expand All @@ -279,11 +282,15 @@ func (client *Client) Apply(resource *resource.Resource, dryMode bool) (string,
return upsertResponse.UpsertResult, nil
}

func (client *Client) Get(kind *schema.Kind, parentPathValue []string, queryParams map[string]string) ([]resource.Resource, error) {
func (client *Client) Get(kind *schema.Kind, parentPathValue []string, parentQueryValue []string, queryParams map[string]string) ([]resource.Resource, error) {
var result []resource.Resource
client.setAuthMethodFromEnvIfNeeded()
url := client.baseUrl + kind.ListPath(parentPathValue)
queryInfo := kind.ListPath(parentPathValue, parentQueryValue)
url := client.baseUrl + queryInfo.Path
requestBuilder := client.client.R()
for _, p := range queryInfo.QueryParams {
requestBuilder = requestBuilder.SetQueryParam(p.Name, p.Value)
}
if queryParams != nil {
requestBuilder = requestBuilder.SetQueryParams(queryParams)
}
Expand Down Expand Up @@ -318,11 +325,16 @@ func (client *Client) Login(username, password string) (LoginResult, error) {
return result, nil
}

func (client *Client) Describe(kind *schema.Kind, parentPathValue []string, name string) (resource.Resource, error) {
func (client *Client) Describe(kind *schema.Kind, parentPathValue []string, parentQueryValue []string, name string) (resource.Resource, error) {
var result resource.Resource
client.setAuthMethodFromEnvIfNeeded()
url := client.baseUrl + kind.DescribePath(parentPathValue, name)
resp, err := client.client.R().Get(url)
queryInfo := kind.DescribePath(parentPathValue, parentQueryValue, name)
url := client.baseUrl + queryInfo.Path
requestBuilder := client.client.R()
for _, p := range queryInfo.QueryParams {
requestBuilder = requestBuilder.SetQueryParam(p.Name, p.Value)
}
resp, err := requestBuilder.Get(url)
if err != nil {
return result, err
} else if resp.IsError() {
Expand All @@ -332,10 +344,15 @@ func (client *Client) Describe(kind *schema.Kind, parentPathValue []string, name
return result, err
}

func (client *Client) Delete(kind *schema.Kind, parentPathValue []string, name string) error {
func (client *Client) Delete(kind *schema.Kind, parentPathValue []string, parentQueryValue []string, name string) error {
client.setAuthMethodFromEnvIfNeeded()
url := client.baseUrl + kind.DescribePath(parentPathValue, name)
resp, err := client.client.R().Delete(url)
queryInfo := kind.DescribePath(parentPathValue, parentQueryValue, name)
url := client.baseUrl + queryInfo.Path
requestBuilder := client.client.R()
for _, p := range queryInfo.QueryParams {
requestBuilder = requestBuilder.SetQueryParam(p.Name, p.Value)
}
resp, err := requestBuilder.Delete(url)
if err != nil {
return err
} else if resp.IsError() {
Expand Down
45 changes: 31 additions & 14 deletions client/gateway_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,14 @@ func MakeGatewayClientFromEnv() (*GatewayClient, error) {
return client, nil
}

func (client *GatewayClient) Get(kind *schema.Kind, parentPathValue []string, queryParams map[string]string) ([]resource.Resource, error) {
func (client *GatewayClient) Get(kind *schema.Kind, parentPathValue []string, parentQueryValue []string, queryParams map[string]string) ([]resource.Resource, error) {
var result []resource.Resource
url := client.baseUrl + kind.ListPath(parentPathValue)
queryInfo := kind.ListPath(parentPathValue, parentQueryValue)
url := client.baseUrl + queryInfo.Path
requestBuilder := client.client.R()
for _, p := range queryInfo.QueryParams {
requestBuilder = requestBuilder.SetQueryParam(p.Name, p.Value)
}
if queryParams != nil {
requestBuilder = requestBuilder.SetQueryParams(queryParams)
}
Expand All @@ -93,10 +97,15 @@ func (client *GatewayClient) Get(kind *schema.Kind, parentPathValue []string, qu
return result, err
}

func (client *GatewayClient) Describe(kind *schema.Kind, parentPathValue []string, name string) (resource.Resource, error) {
func (client *GatewayClient) Describe(kind *schema.Kind, parentPathValue []string, parentQueryValue []string, name string) (resource.Resource, error) {
var result resource.Resource
url := client.baseUrl + kind.DescribePath(parentPathValue, name)
resp, err := client.client.R().Get(url)
queryInfo := kind.DescribePath(parentPathValue, parentQueryValue, name)
url := client.baseUrl + queryInfo.Path
requestBuilder := client.client.R()
for _, p := range queryInfo.QueryParams {
requestBuilder = requestBuilder.SetQueryParam(p.Name, p.Value)
}
resp, err := requestBuilder.Get(url)
if err != nil {
return result, err
} else if resp.IsError() {
Expand All @@ -106,9 +115,14 @@ func (client *GatewayClient) Describe(kind *schema.Kind, parentPathValue []strin
return result, err
}

func (client *GatewayClient) Delete(kind *schema.Kind, parentPathValue []string, name string) error {
url := client.baseUrl + kind.DescribePath(parentPathValue, name)
resp, err := client.client.R().Delete(url)
func (client *GatewayClient) Delete(kind *schema.Kind, parentPathValue []string, parentQueryValue []string, name string) error {
queryInfo := kind.DescribePath(parentPathValue, parentQueryValue, name)
url := client.baseUrl + queryInfo.Path
requestBuilder := client.client.R()
for _, p := range queryInfo.QueryParams {
requestBuilder = requestBuilder.SetQueryParam(p.Name, p.Value)
}
resp, err := requestBuilder.Delete(url)
if err != nil {
return err
} else if resp.IsError() {
Expand Down Expand Up @@ -154,8 +168,8 @@ func (client *GatewayClient) DeleteResourceByNameAndVCluster(resource *resource.
if !ok {
return fmt.Errorf("kind %s not found", resource.Kind)
}
deletePath := kind.ListPath(nil)
url := client.baseUrl + deletePath
deletePath := kind.ListPath(nil, nil)
url := client.baseUrl + deletePath.Path
resp, err := client.client.R().SetBody(map[string]string{"name": name, "vCluster": vCluster.(string)}).Delete(url)
if err != nil {
return err
Expand Down Expand Up @@ -226,7 +240,7 @@ func (client *GatewayClient) DeleteResourceInterceptors(resource *resource.Resou
}

func (client *GatewayClient) DeleteKindByNameAndVCluster(kind *schema.Kind, param map[string]string) error {
url := client.baseUrl + kind.ListPath(nil)
url := client.baseUrl + kind.ListPath(nil, nil).Path
req := client.client.R()
req.SetBody(param)
resp, err := req.Delete(url)
Expand All @@ -242,7 +256,7 @@ func (client *GatewayClient) DeleteKindByNameAndVCluster(kind *schema.Kind, para
}

func (client *GatewayClient) DeleteInterceptor(kind *schema.Kind, name string, param map[string]string) error {
url := client.baseUrl + kind.ListPath(nil) + "/" + name
url := client.baseUrl + kind.ListPath(nil, nil).Path + "/" + name
req := client.client.R()
var bodyParams = make(map[string]interface{})
for k, v := range param {
Expand Down Expand Up @@ -275,12 +289,15 @@ func (client *GatewayClient) Apply(resource *resource.Resource, dryMode bool) (s
if !ok {
return "", fmt.Errorf("kind %s not found", resource.Kind)
}
applyPath, err := kind.ApplyPath(resource)
applyQueryInfo, err := kind.ApplyPath(resource)
if err != nil {
return "", err
}
url := client.baseUrl + applyPath
url := client.baseUrl + applyQueryInfo.Path
builder := client.client.R().SetBody(resource.Json)
for _, param := range applyQueryInfo.QueryParams {
builder = builder.SetQueryParam(param.Name, param.Value)
}
if dryMode {
builder = builder.SetQueryParam("dryMode", "true")
}
Expand Down
8 changes: 4 additions & 4 deletions client/gateway_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func TestGwGetShouldWork(t *testing.T) {
)

vClusterKind := gatewayClient.GetKinds()["VirtualCluster"]
result, err := gatewayClient.Get(&vClusterKind, []string{}, nil)
result, err := gatewayClient.Get(&vClusterKind, []string{}, []string{}, nil)
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -191,7 +191,7 @@ func TestGwGetShouldFailIfN2xx(t *testing.T) {
)

vClusterKind := gatewayClient.GetKinds()["VirtualCluster"]
_, err = gatewayClient.Get(&vClusterKind, []string{}, nil)
_, err = gatewayClient.Get(&vClusterKind, []string{}, []string{}, nil)
if err == nil {
t.Failed()
}
Expand Down Expand Up @@ -227,7 +227,7 @@ func TestGwDeleteShouldWork(t *testing.T) {
)

vClusters := gatewayClient.GetKinds()["VirtualCluster"]
err = gatewayClient.Delete(&vClusters, []string{}, "vcluster1")
err = gatewayClient.Delete(&vClusters, []string{}, []string{}, "vcluster1")
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -262,7 +262,7 @@ func TestGwDeleteShouldFailOnNot2XX(t *testing.T) {
)

vClusterKind := gatewayClient.GetKinds()["VirtualCluster"]
err = gatewayClient.Delete(&vClusterKind, []string{}, "vcluster1")
err = gatewayClient.Delete(&vClusterKind, []string{}, []string{}, "vcluster1")
if err == nil {
t.Fail()
}
Expand Down
14 changes: 12 additions & 2 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,29 @@ func initDelete(kinds schema.KindCatalog, strict bool) {
deleteCmd.AddCommand(interceptorsDeleteCmd)
} else {
flags := kind.GetParentFlag()
parentQueryFlags := kind.GetParentQueryFlag()
parentFlagValue := make([]*string, len(flags))
parentQueryFlagValue := make([]*string, len(parentQueryFlags))
kindCmd := &cobra.Command{
Use: fmt.Sprintf("%s [name]", name),
Short: "Delete resource of kind " + name,
Args: cobra.MatchAll(cobra.ExactArgs(1)),
Aliases: buildAlias(name),
Run: func(cmd *cobra.Command, args []string) {
parentValue := make([]string, len(parentFlagValue))
parentQueryValue := make([]string, len(parentQueryFlagValue))
for i, v := range parentFlagValue {
parentValue[i] = *v
}
for i, v := range parentQueryFlagValue {
parentQueryValue[i] = *v
}

var err error
if isGatewayKind(kind) {
err = gatewayApiClient().Delete(&kind, parentValue, args[0])
err = gatewayApiClient().Delete(&kind, parentValue, parentQueryValue, args[0])
} else {
err = consoleApiClient().Delete(&kind, parentValue, args[0])
err = consoleApiClient().Delete(&kind, parentValue, parentQueryValue, args[0])
}
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
Expand All @@ -87,6 +94,9 @@ func initDelete(kinds schema.KindCatalog, strict bool) {
parentFlagValue[i] = kindCmd.Flags().String(flag, "", "Parent "+flag)
kindCmd.MarkFlagRequired(flag)
}
for i, flag := range parentQueryFlags {
parentQueryFlagValue[i] = kindCmd.Flags().String(flag, "", "Parent "+flag)
}
deleteCmd.AddCommand(kindCmd)
}
}
Expand Down
Loading

0 comments on commit 0d5dc6d

Please sign in to comment.