Skip to content

Commit

Permalink
add data provider cf_service_instance
Browse files Browse the repository at this point in the history
  • Loading branch information
jcarrothers-sap committed Apr 6, 2018
1 parent b5755e7 commit 6273800
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 14 deletions.
3 changes: 2 additions & 1 deletion cloudfoundry/cfapi/service_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ func (sm *ServiceManager) ReadServiceInstance(serviceInstanceID string) (service
}

// FindServiceInstance -
func (sm *ServiceManager) FindServiceInstance(name string, spaceID string) (serviceInstance CCServiceInstance, err error) {
func (sm *ServiceManager) FindServiceInstance(name string, spaceID string) (guid string, serviceInstance CCServiceInstance, err error) {

path := fmt.Sprintf("/v2/spaces/%s/service_instances?return_user_provided_service_instances=true&q=%s&inline-relations-depth=1",
spaceID, url.QueryEscape("name:"+name))
Expand All @@ -487,6 +487,7 @@ func (sm *ServiceManager) FindServiceInstance(name string, spaceID string) (serv
func(resource interface{}) bool {
if sp, ok := resource.(CCServiceInstanceResource); ok {
serviceInstance = sp.Entity // there should 1 or 0 instances in the space with that name
guid = sp.Metadata.GUID
found = true
return false
}
Expand Down
81 changes: 81 additions & 0 deletions cloudfoundry/data_source_cf_service_instance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package cloudfoundry

import (
"fmt"

"github.com/satori/go.uuid"

"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-cf/cloudfoundry/cfapi"
)

func dataSourceServiceInstance() *schema.Resource {

return &schema.Resource{

Read: dataSourceServiceInstanceRead,

Schema: map[string]*schema.Schema{

"name_or_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"space": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"service_plan_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"tags": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}

func dataSourceServiceInstanceRead(d *schema.ResourceData, meta interface{}) (err error) {

session := meta.(*cfapi.Session)
if session == nil {
return fmt.Errorf("client is nil")
}

sm := session.ServiceManager()

var (
name_or_id string
space string
guid string
serviceInstance cfapi.CCServiceInstance
)

name_or_id = d.Get("name_or_id").(string)
space = d.Get("space").(string)

isUUID := uuid.FromStringOrNil(name_or_id)
if &isUUID == nil || uuid.Equal(isUUID, uuid.Nil) {
guid, serviceInstance, err = sm.FindServiceInstance(name_or_id, space)
} else {
guid = name_or_id
serviceInstance, err = sm.ReadServiceInstance(name_or_id)
}
if err != nil {
return err
}

d.SetId(guid)
d.Set("name", serviceInstance.Name)
d.Set("service_plan_id", serviceInstance.ServicePlanGUID)
d.Set("tags", serviceInstance.Tags)

return
}
2 changes: 1 addition & 1 deletion cloudfoundry/import_cf_service_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func testAccCheckServiceInstanceDestroyedImportState(names []string, serviceInst

session.Log.DebugMessage("checking ServiceInstance is Destroyed %s", n)

if _, err := session.ServiceManager().FindServiceInstance(n, spaceId); err != nil {
if _, _, err := session.ServiceManager().FindServiceInstance(n, spaceId); err != nil {
switch err.(type) {
case *errors.ModelNotFoundError:
return nil
Expand Down
21 changes: 11 additions & 10 deletions cloudfoundry/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,17 @@ func Provider() terraform.ResourceProvider {
},

DataSourcesMap: map[string]*schema.Resource{
"cf_info": dataSourceInfo(),
"cf_stack": dataSourceStack(),
"cf_router_group": dataSourceRouterGroup(),
"cf_user": dataSourceUser(),
"cf_domain": dataSourceDomain(),
"cf_asg": dataSourceAsg(),
"cf_quota": dataSourceQuota(),
"cf_org": dataSourceOrg(),
"cf_space": dataSourceSpace(),
"cf_service": dataSourceService(),
"cf_info": dataSourceInfo(),
"cf_stack": dataSourceStack(),
"cf_router_group": dataSourceRouterGroup(),
"cf_user": dataSourceUser(),
"cf_domain": dataSourceDomain(),
"cf_asg": dataSourceAsg(),
"cf_quota": dataSourceQuota(),
"cf_org": dataSourceOrg(),
"cf_space": dataSourceSpace(),
"cf_service": dataSourceService(),
"cf_service_instance": dataSourceServiceInstance(),
},

ResourcesMap: map[string]*schema.Resource{
Expand Down
2 changes: 1 addition & 1 deletion cloudfoundry/resource_cf_service_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func testAccCheckServiceInstanceDestroyed(names []string, spaceResource string)

session.Log.DebugMessage("checking ServiceInstance is Destroyed %s", n)

if _, err := session.ServiceManager().FindServiceInstance(n, rs.Primary.ID); err != nil {
if _, _, err := session.ServiceManager().FindServiceInstance(n, rs.Primary.ID); err != nil {
switch err.(type) {
case *errors.ModelNotFoundError:
return nil
Expand Down
2 changes: 1 addition & 1 deletion cloudfoundry/resource_cf_user_provided_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func testAccCheckUserProvidedServiceDestroyed(name string, spaceResource string)

session.Log.DebugMessage("checking User Provided Service is Destroyed %s", name)

if _, err := session.ServiceManager().FindServiceInstance(name, rs.Primary.ID); err != nil {
if _, _, err := session.ServiceManager().FindServiceInstance(name, rs.Primary.ID); err != nil {
switch err.(type) {
case *errors.ModelNotFoundError:
return nil
Expand Down

0 comments on commit 6273800

Please sign in to comment.