Skip to content
This repository has been archived by the owner on Aug 6, 2021. It is now read-only.

[WIP] Allow disabling service access. #18

Open
wants to merge 1 commit into
base: master
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ resource "cloudfoundry_service_broker" "service_broker_mysuperbroker" {
- **service**: (**Required**) Service name from your service broker catalog to activate. **Note**: if there is only service in your service access it will enable all plan on all orgs on your Cloud Foundry.
- **plan**: *(Optional, default: `null`)* Plan from your service broker catalog attached to this service to activate. **Note**: if no `org_id` is given it will enable this plan on all orgs.
- **org_id**: *(Optional, default: `null`)* Org id created from resource [cloudfoundry_organization](#organizations) to activate this service. **Note**: if no `plan` is given it will all plans on this org.
- **disable**: *(Optional, default: `false`)* Disable service access rather than enabling.

**BUG FOUND**: if you set both `plan` and `org_id` in your `service_access` Cloud Foundry will enable all plans on this org. It's maybe only on the version of Cloud Foundry I am. Feedbacks are needed on other versions.

Expand Down
36 changes: 24 additions & 12 deletions resources/service_broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package resources

import (
"bytes"
"code.cloudfoundry.org/cli/cf/models"
"errors"
"fmt"
"log"
"strings"

"code.cloudfoundry.org/cli/cf/models"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema"
"github.com/orange-cloudfoundry/terraform-provider-cloudfoundry/cf_client"
"log"
"strings"
)

type CfServiceBrokerResource struct {
Expand All @@ -19,6 +20,7 @@ type ServiceAccess struct {
Service string
Plan string
OrgId string
Disable bool
}

func (c CfServiceBrokerResource) serviceAccessObjects(d *schema.ResourceData) []ServiceAccess {
Expand All @@ -37,6 +39,7 @@ func (c CfServiceBrokerResource) serviceAccessObject(serviceAccessMap map[string
Service: serviceAccessMap["service"].(string),
Plan: serviceAccessMap["plan"].(string),
OrgId: serviceAccessMap["org_id"].(string),
Disable: serviceAccessMap["disable"].(bool),
}
}
func (c CfServiceBrokerResource) getFullServicesAccessDef(client cf_client.Client, serviceBroker models.ServiceBroker, servicesAccess []ServiceAccess) ([]ServiceAccess, error) {
Expand Down Expand Up @@ -290,6 +293,7 @@ func (c CfServiceBrokerResource) getServicesAccessDefWithOnlyOrg(service models.
Service: serviceAccess.Service,
OrgId: serviceAccess.OrgId,
Plan: plan.Name,
Disable: serviceAccess.Disable,
})
}
return servicesAccess
Expand All @@ -305,6 +309,7 @@ func (c CfServiceBrokerResource) getServicesAccessDefWithOnlyPlan(client cf_clie
Service: serviceAccess.Service,
OrgId: org.GUID,
Plan: serviceAccess.Plan,
Disable: serviceAccess.Disable,
})
}
return servicesAccess, nil
Expand Down Expand Up @@ -402,27 +407,34 @@ func (c CfServiceBrokerResource) diffServicesAccess(client cf_client.Client, ser
toDelete = make([]ServiceAccess, 0)
toCreate = make([]ServiceAccess, 0)

servicesAccessMapSrc := c.makeServicesAccessMap(servicesAccessSrc)
servicesAccessMapDest := c.makeServicesAccessMap(servicesAccessDest)

for _, serviceAccessSrc := range servicesAccessSrc {
if !c.containsServiceAccess(servicesAccessDest, serviceAccessSrc) {
exists := c.containsServiceAccess(servicesAccessMapDest, serviceAccessSrc)
if (!exists && !serviceAccessSrc.Disable) ||
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the condition just should be if !exists || serviceAccessSrc.Disable, am I missing something ?

(exists && serviceAccessSrc.Disable) {
toDelete = append(toDelete, serviceAccessSrc)
}
}
for _, serviceAccessDest := range servicesAccessDest {
if !c.containsServiceAccess(servicesAccessSrc, serviceAccessDest) {
if !c.containsServiceAccess(servicesAccessMapSrc, serviceAccessDest) {
toCreate = append(toCreate, serviceAccessDest)
}
}
return
}
func (c CfServiceBrokerResource) containsServiceAccess(servicesAccess []ServiceAccess, serviceAccessToFind ServiceAccess) bool {
func (c CfServiceBrokerResource) makeServicesAccessMap(servicesAccess []ServiceAccess) map[ServiceAccess]bool {
serviceAccessMap := make(map[ServiceAccess]bool, len(servicesAccess))
for _, serviceAccess := range servicesAccess {
if serviceAccess.OrgId == serviceAccessToFind.OrgId &&
serviceAccess.Plan == serviceAccessToFind.Plan &&
serviceAccess.Service == serviceAccessToFind.Service {
return true
}
serviceAccess.Disable = false
serviceAccessMap[serviceAccess] = true
}
return false
return serviceAccessMap
}
func (c CfServiceBrokerResource) containsServiceAccess(servicesAccess map[ServiceAccess]bool, serviceAccessToFind ServiceAccess) bool {
_, ok := servicesAccess[serviceAccessToFind]
return ok
}
func (c CfServiceBrokerResource) Read(d *schema.ResourceData, meta interface{}) error {
client := meta.(cf_client.Client)
Expand Down