Skip to content

Commit

Permalink
F #476: add marketplace app resource
Browse files Browse the repository at this point in the history
  • Loading branch information
treywelsh committed Oct 11, 2023
1 parent 0cbd9d1 commit 9f35d34
Show file tree
Hide file tree
Showing 8 changed files with 1,068 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ FEATURES:

* **New Resource**: `opennebula_marketplace` (#414)
* ***New Data Source**: `opennebula_marketplace` (#414)
* **New Resource**: `opennebula_marketplace_appliance` (#476)
* **New Data Source**: `opennebula_marketplace_appliance` (#476)

# 1.3.1 (September 11st, 2023)

Expand Down
110 changes: 110 additions & 0 deletions opennebula/data_opennebula_marketplace_app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package opennebula

import (
"context"
"fmt"
"strconv"

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

func dataOpennebulaMarketplaceApp() *schema.Resource {
return &schema.Resource{
ReadContext: datasourceOpennebulaMarketplaceAppRead,

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

func applianceFilter(d *schema.ResourceData, meta interface{}) (*appSc.MarketPlaceApp, error) {

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

apps, err := controller.MarketPlaceApps().Info()
if err != nil {
return nil, err
}

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

match := make([]*appSc.MarketPlaceApp, 0, 1)
for i, app := range apps.MarketPlaceApps {

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

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

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

match = append(match, &apps.MarketPlaceApps[i])
}

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

return match[0], nil
}

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

var diags diag.Diagnostics

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

tplPairs := pairsToMap(app.Template.Template)

d.SetId(strconv.FormatInt(int64(app.ID), 10))
d.Set("name", app.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("Appliance (ID: %d): %s", app.ID, err),
})
return diags
}
}

return nil
}
24 changes: 24 additions & 0 deletions opennebula/helpers_appliance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package opennebula

type ApplianceType string

const (
AppTypeImage = "IMAGE"
AppTypeVM = "VMTEMPLATE"
AppTypeService = "SERVICE_TEMPLATE"
)

func ApplianceTypeToString(appType int) string {
switch appType {
case 0:
return "UNKNOWN"
case 1:
return "IMAGE"
case 2:
return "VMTEMPLATE"
case 3:
return "SERVICE_TEMPLATE"
default:
return ""
}
}
1 change: 1 addition & 0 deletions opennebula/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ func Provider() *schema.Provider {
"opennebula_host": resourceOpennebulaHost(),
"opennebula_datastore": resourceOpennebulaDatastore(),
"opennebula_marketplace": resourceOpennebulaMarketPlace(),
"opennebula_marketplace_appliance": resourceOpennebulaMarketPlaceApp(),
},

ConfigureContextFunc: providerConfigure,
Expand Down
12 changes: 4 additions & 8 deletions opennebula/resource_opennebula_marketplace.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,17 +293,13 @@ func resourceOpennebulaMarketPlace() *schema.Resource {
func getMarketPlaceController(d *schema.ResourceData, meta interface{}) (*goca.MarketPlaceController, error) {
config := meta.(*Configuration)
controller := config.Controller
var gc *goca.MarketPlaceController

if d.Id() != "" {
gid, err := strconv.ParseUint(d.Id(), 10, 0)
if err != nil {
return nil, err
}
gc = controller.MarketPlace(int(gid))
marketID, err := strconv.ParseUint(d.Id(), 10, 0)
if err != nil {
return nil, err
}

return gc, nil
return controller.MarketPlace(int(marketID)), nil
}

func resourceOpennebulaMarketPlaceCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
Expand Down
Loading

0 comments on commit 9f35d34

Please sign in to comment.