-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
F #476: add marketplace app resource
- Loading branch information
Showing
8 changed files
with
1,068 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.