Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add parameters to subscription #25

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions apis/account/v1alpha1/subscription_types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package v1alpha1

import (
"encoding/json"
"reflect"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -27,6 +28,8 @@ type SubscriptionParameters struct {
// PlanName to subscribe to
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="planName can't be updated once set"
PlanName string `json:"planName"`
// Subscription parameters allows you to add additional parameters
SubscriptionParameters json.RawMessage `json:"subscriptionParameters"`
}

// SubscriptionObservation are the observable fields of a Subscription.
Expand Down
8 changes: 7 additions & 1 deletion apis/account/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion internal/clients/subscription/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package subscription

import (
"context"
"encoding/json"
"fmt"
"strings"

Expand Down Expand Up @@ -156,11 +157,24 @@ func (s *SubscriptionTypeMapper) ConvertToCreatePayload(cr *v1alpha1.Subscriptio
return SubscriptionPost{
appName: cr.Spec.ForProvider.AppName,
CreateSubscriptionRequestPayload: saas_client.CreateSubscriptionRequestPayload{
PlanName: &cr.Spec.ForProvider.PlanName,
PlanName: &cr.Spec.ForProvider.PlanName,
SubscriptionParams: s.ConvertToClientParams(cr),
},
}
}

func (s *SubscriptionTypeMapper) ConvertToClientParams(cr *v1alpha1.Subscription) map[string]map[string]interface{} {
type subparams map[string]map[string]interface{}
var subscriptionParams subparams

err := json.Unmarshal(cr.Spec.ForProvider.SubscriptionParameters, &subscriptionParams)
if err != nil {
return nil
}

return subscriptionParams
}

func (s *SubscriptionTypeMapper) ConvertToUpdatePayload(cr *v1alpha1.Subscription) SubscriptionPut {
panic("currently not supported")
}
Expand Down
51 changes: 45 additions & 6 deletions internal/clients/subscription/subscription_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package subscription

import (
"context"
"encoding/json"
"net/http"
"testing"

Expand Down Expand Up @@ -103,6 +104,14 @@ func TestSubscriptionApiHandler_CreateSubscription(t *testing.T) {
appName: "name1",
CreateSubscriptionRequestPayload: saas_client.CreateSubscriptionRequestPayload{
PlanName: internal.Ptr("plan2"),
SubscriptionParams: map[string]map[string]interface{}{
"param1": {
"key1": "value1",
},
"param2": {
"key2": "value2",
},
},
},
},
mockSubscriptionApi: apiMockPOST(
Expand All @@ -117,6 +126,14 @@ func TestSubscriptionApiHandler_CreateSubscription(t *testing.T) {
appName: "name1",
CreateSubscriptionRequestPayload: saas_client.CreateSubscriptionRequestPayload{
PlanName: internal.Ptr("plan2"),
SubscriptionParams: map[string]map[string]interface{}{
"param1": {
"key1": "value1",
},
"param2": {
"key2": "value2",
},
},
},
},
mockSubscriptionApi: apiMockPOST(
Expand Down Expand Up @@ -247,7 +264,8 @@ func TestSubscriptionApiHandler_UpdateSubscription(t *testing.T) {
}

func TestSubscriptionTypeMapper_ConvertToCreatePayload(t *testing.T) {
cr := NewSubscription("someName", "name1", "plan2")
raw := json.RawMessage(`{"name": "John", "age": 30}`)
cr := NewSubscription("someName", "name1", "plan2", raw)

uut := NewSubscriptionTypeMapper()
mapped := uut.ConvertToCreatePayload(cr)
Expand All @@ -258,8 +276,27 @@ func TestSubscriptionTypeMapper_ConvertToCreatePayload(t *testing.T) {

}

func TestSubscriptionTypeMapper_ConvertToClientParams(t *testing.T) {
raw := json.RawMessage(`{"param1": {"key1": "value1"}, "param2": {"key2": "value2"}}`)
cr := &v1alpha1.Subscription{
Spec: v1alpha1.SubscriptionSpec{
ForProvider: v1alpha1.SubscriptionParameters{
SubscriptionParameters: raw,
},
},
}

uut := NewSubscriptionTypeMapper()
params := uut.ConvertToClientParams(cr)

assert.NotNil(t, params)
assert.Equal(t, "value1", params["param1"]["key1"])
assert.Equal(t, "value2", params["param2"]["key2"])
}

func TestSubscriptionTypeMapper_IsSynced(t *testing.T) {
cr := NewSubscription("someName", "name1", "plan2")
raw := json.RawMessage(`{"name": "John", "age": 30}`)
cr := NewSubscription("someName", "name1", "plan2", raw)
get := &SubscriptionGet{
AppName: internal.Ptr("anotherName"),
PlanName: internal.Ptr("anotherPlan"),
Expand Down Expand Up @@ -310,14 +347,15 @@ func TestSubscriptionTypeMapper_IsAvailable(t *testing.T) {
}

func TestSubscriptionTypeMapper_SyncStatus(t *testing.T) {
raw := json.RawMessage(`{"name": "John", "age": 30}`)
tests := map[string]struct {
cr *v1alpha1.Subscription
apiRes *SubscriptionGet

expectedCr *v1alpha1.Subscription
}{
"SetState": {
cr: NewSubscription("someName", "name1", "plan2"),
cr: NewSubscription("someName", "name1", "plan2", raw),
apiRes: &SubscriptionGet{
AppName: internal.Ptr("name1"),
PlanName: internal.Ptr("plan2"),
Expand Down Expand Up @@ -374,13 +412,14 @@ func apiMockDELETE(statusCode int, apiError error) *MockSubscriptionOperationsCo
return apiMock
}

func NewSubscription(crName string, appName string, planName string) *v1alpha1.Subscription {
func NewSubscription(crName string, appName string, planName string, subscriptionParameters json.RawMessage) *v1alpha1.Subscription {
cr := &v1alpha1.Subscription{
ObjectMeta: metav1.ObjectMeta{Name: crName},
Spec: v1alpha1.SubscriptionSpec{
ForProvider: v1alpha1.SubscriptionParameters{
AppName: appName,
PlanName: planName,
AppName: appName,
PlanName: planName,
SubscriptionParameters: subscriptionParameters,
},
},
}
Expand Down
6 changes: 6 additions & 0 deletions package/crds/account.btp.sap.crossplane.io_subscriptions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,15 @@ spec:
x-kubernetes-validations:
- message: planName can't be updated once set
rule: self == oldSelf
subscriptionParameters:
description: Subscription parameters allows you to add additional
parameters
format: byte
type: string
required:
- appName
- planName
- subscriptionParameters
type: object
managementPolicies:
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ spec:
forProvider:
appName: auditlog-viewer
planName: free
subscriptionParameters: {"key1": "value1","key2": {"key3":"value3"}}
cloudManagementRef:
name: e2e-sub-cis-local
Loading