Skip to content

Commit

Permalink
F #414: add marketplace datasource
Browse files Browse the repository at this point in the history
  • Loading branch information
treywelsh committed Apr 18, 2023
1 parent e2e4b04 commit 9a5f08a
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 0 deletions.
110 changes: 110 additions & 0 deletions opennebula/data_opennebula_marketplace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package opennebula

import (
"context"
"fmt"
"strconv"

marketplaceSc "github.com/OpenNebula/one/src/oca/go/src/goca/schemas/marketplace"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataOpennebulaMarketplace() *schema.Resource {
return &schema.Resource{
ReadContext: datasourceOpennebulaMarketplaceRead,

Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Optional: true,
Default: -1,
Description: "Id of the marketplace",
},
"name": {
Type: schema.TypeString,
Optional: true,
Description: "Name of the marketplace",
},
"tags": tagsSchema(),
},
}
}

func marketplaceFilter(d *schema.ResourceData, meta interface{}) (*marketplaceSc.MarketPlace, error) {

config := meta.(*Configuration)
controller := config.Controller

marketplaces, err := controller.MarketPlaces().Info()
if err != nil {
return nil, err
}

// filter marketplaces with user defined criterias
id := d.Get("id")
name, nameOk := d.GetOk("name")
tagsInterface, tagsOk := d.GetOk("tags")
tags := tagsInterface.(map[string]interface{})

match := make([]*marketplaceSc.MarketPlace, 0, 1)
for i, marketplace := range marketplaces.MarketPlaces {

if id != -1 && marketplace.ID != id {
continue
}

if nameOk && marketplace.Name != name {
continue
}

if tagsOk && !matchTags(marketplace.Template.Template, tags) {
continue
}

match = append(match, &marketplaces.MarketPlaces[i])
}

// check filtering results
if len(match) == 0 {
return nil, fmt.Errorf("no marketplace match the constraints")
} else if len(match) > 1 {
return nil, fmt.Errorf("several marketplaces match the constraints")
}

return match[0], nil
}

func datasourceOpennebulaMarketplaceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {

var diags diag.Diagnostics

marketplace, err := marketplaceFilter(d, meta)
if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "marketplaces filtering failed",
Detail: err.Error(),
})
return diags
}

tplPairs := pairsToMap(marketplace.Template.Template)

d.SetId(strconv.FormatInt(int64(marketplace.ID), 10))
d.Set("name", marketplace.Name)

if len(tplPairs) > 0 {
err := d.Set("tags", tplPairs)
if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "setting attribute failed",
Detail: fmt.Sprintf("Marketplace (ID: %d): %s", marketplace.ID, err),
})
return diags
}
}

return nil
}
1 change: 1 addition & 0 deletions opennebula/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func Provider() *schema.Provider {
"opennebula_host": dataOpennebulaHost(),
"opennebula_datastore": dataOpennebulaDatastore(),
"opennebula_zone": dataOpennebulaZone(),
"opennebula_marketplace": dataOpennebulaMarketplace(),
},

ResourcesMap: map[string]*schema.Resource{
Expand Down
31 changes: 31 additions & 0 deletions website/docs/d/marketplace.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
layout: "opennebula"
page_title: "OpenNebula: opennebula_marketplace"
sidebar_current: "docs-opennebula-datasource-marketplace"
description: |-
Get the marketplace information for a given name.
---

# opennebula_marketplace

Use this data source to retrieve the marketplace information from it's name or tags.

## Example Usage

```hcl
data "opennebula_marketplace" "example" {
name = "My_Marketplace"
}
```

## Argument Reference

* `id` - (Optional) ID of the marketplace.
* `name` - (Optional) The OpenNebula marketplace to retrieve information for.
* `tags` - (Optional) Tags associated to the marketplace.

## Attribute Reference

* `id` - ID of the marketplace.
* `name` - The OpenNebula marketplace name.
* `tags` - Tags of the marketplace (Key = Value).

0 comments on commit 9a5f08a

Please sign in to comment.