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

[Backport 643] Make application plans publication state configurable #648

Merged
Merged
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
9 changes: 9 additions & 0 deletions apis/capabilities/v1beta1/product_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,18 @@ type ApplicationPlanSpec struct {
// +optional
Limits []LimitSpec `json:"limits,omitempty"`

// Controls whether the application plan is published. If not specified it is
// hidden by default
// +optional
Published *bool `json:"published,omitempty"`

// TODO Features
}

func (a *ApplicationPlanSpec) IsPublished() bool {
return a.Published != nil && *a.Published
}

// MethodSpec defines the desired state of Product's Method
type MethodSpec struct {
Name string `json:"friendlyName"`
Expand Down
5 changes: 5 additions & 0 deletions apis/capabilities/v1beta1/zz_generated.deepcopy.go

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

3 changes: 3 additions & 0 deletions bundle/manifests/capabilities.3scale.net_products.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ spec:
- to
type: object
type: array
published:
description: Controls whether the application plan is published. If not specified it is hidden by default
type: boolean
setupFee:
description: Setup fee (USD)
pattern: ^\d+(\.\d{2})?$
Expand Down
4 changes: 4 additions & 0 deletions config/crd/bases/capabilities.3scale.net_products.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ spec:
- to
type: object
type: array
published:
description: Controls whether the application plan is published.
If not specified it is hidden by default
type: boolean
setupFee:
description: Setup fee (USD)
pattern: ^\d+(\.\d{2})?$
Expand Down
12 changes: 12 additions & 0 deletions controllers/capabilities/application_plan_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,18 @@ func (a *applicationPlanReconciler) syncPlan(_ interface{}) error {
}
}

planEntityStateIsPublished := a.planEntity.State() == "published" // If the state is not published then we assume it is "hidden"
desiredApplicationPlanIsPublished := a.resource.IsPublished()
if planEntityStateIsPublished != desiredApplicationPlanIsPublished {
var stateEventValue string
if desiredApplicationPlanIsPublished {
stateEventValue = "publish"
} else {
stateEventValue = "hide"
}
params["state_event"] = stateEventValue
}

if len(params) > 0 {
err := a.planEntity.Update(params)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions doc/product-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ Specifies product mapping rules
| Metric Method Reference | `metricMethodRef` | string | Existing method or metric **system name** | Yes |
| Increment | `increment` | int | Increase the metric by this delta | Yes |
| Last | `last` | \*bool | Last matched Mapping Rule to process | No |
| Published | `published` | \*bool | Controls whether the application plan is published. If not specified it is hidden by default | No |

#### MetricSpec

Expand Down
4 changes: 4 additions & 0 deletions pkg/controller/helper/application_plan_entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func (b *ApplicationPlanEntity) CostPerMonth() float64 {
return b.obj.CostPerMonth
}

func (b *ApplicationPlanEntity) State() string {
return b.obj.State
}

func (b *ApplicationPlanEntity) Update(params threescaleapi.Params) error {
b.logger.V(1).Info("Update", "params", params)
updated, err := b.client.UpdateApplicationPlan(b.productID, b.obj.ID, params)
Expand Down
4 changes: 4 additions & 0 deletions pkg/controller/helper/application_plan_entity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func TestApplicationPlanEntityBasics(t *testing.T) {
TrialPeriodDays: 3,
SetupFee: 5.67,
CostPerMonth: 8.67,
State: "hide",
}

client := threescaleapi.NewThreeScale(nil, token, nil)
Expand All @@ -33,6 +34,7 @@ func TestApplicationPlanEntityBasics(t *testing.T) {
equals(t, appPlanEntity.TrialPeriodDays(), planItem.TrialPeriodDays)
equals(t, appPlanEntity.SetupFee(), planItem.SetupFee)
equals(t, appPlanEntity.CostPerMonth(), planItem.CostPerMonth)
equals(t, appPlanEntity.State(), planItem.State)
}

func TestApplicationPlanEntityUpdate(t *testing.T) {
Expand All @@ -48,6 +50,7 @@ func TestApplicationPlanEntityUpdate(t *testing.T) {
TrialPeriodDays: 3,
SetupFee: 5.67,
CostPerMonth: 8.67,
State: "publish",
},
}

Expand All @@ -72,6 +75,7 @@ func TestApplicationPlanEntityUpdate(t *testing.T) {
equals(t, appPlanEntity.TrialPeriodDays(), 3)
equals(t, appPlanEntity.SetupFee(), 5.67)
equals(t, appPlanEntity.CostPerMonth(), 8.67)
equals(t, appPlanEntity.State(), "publish")
}

func TestApplicationPlanEntityUpdateError(t *testing.T) {
Expand Down