Skip to content

Commit

Permalink
F #414: add marketplace resource
Browse files Browse the repository at this point in the history
  • Loading branch information
treywelsh committed Sep 18, 2023
1 parent 5fafae2 commit 3c49a15
Show file tree
Hide file tree
Showing 9 changed files with 1,519 additions and 4 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# 1.4.0 (Unreleased)

FEATURES:

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

# 1.3.1 (September 11st, 2023)

BUG FIXES:
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/OpenNebula/terraform-provider-opennebula
go 1.18

require (
github.com/OpenNebula/one/src/oca/go/src/goca v0.0.0-20230301133003-197f04efa071
github.com/OpenNebula/one/src/oca/go/src/goca v0.0.0-20230912160253-4673721a21ca
github.com/hashicorp/go-uuid v1.0.3
github.com/hashicorp/go-version v1.6.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.24.1
Expand Down Expand Up @@ -34,6 +34,7 @@ require (
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 // indirect
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d // indirect
github.com/kolo/xmlrpc v0.0.0-20190909154602-56d5ec7c422e // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
Expand Down
10 changes: 7 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
github.com/Microsoft/go-winio v0.4.14/go.mod h1:qXqCSQ3Xa7+6tgxaGTIe4Kpcdsi+P8jBhyzoq1bpyYA=
github.com/Microsoft/go-winio v0.4.16 h1:FtSW/jqD+l4ba5iPBj9CODVtgfYAD8w2wS923g/cFDk=
github.com/Microsoft/go-winio v0.4.16/go.mod h1:XB6nPKklQyQ7GC9LdcBEcBl8PF76WugXOPRXwdLnMv0=
github.com/OpenNebula/one/src/oca/go/src/goca v0.0.0-20230301133003-197f04efa071 h1:qxsdY1BZDslfn3RIpfLwirhj1SCrlK/vD9gVqMIiYv0=
github.com/OpenNebula/one/src/oca/go/src/goca v0.0.0-20230301133003-197f04efa071/go.mod h1:dvAwZi1Aol7eu6BENzHtl8ztGBkacB9t/fJj+fYk+Xg=
github.com/OpenNebula/one/src/oca/go/src/goca v0.0.0-20230912160253-4673721a21ca h1:0d7BySuGPBL+/7kVTW/29kJJhPLCFW0w4nLBaHI14jc=
github.com/OpenNebula/one/src/oca/go/src/goca v0.0.0-20230912160253-4673721a21ca/go.mod h1:dvAwZi1Aol7eu6BENzHtl8ztGBkacB9t/fJj+fYk+Xg=
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 h1:YoJbenK9C67SkzkDfmQuVln04ygHj3vjZfd9FL+GmQQ=
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7/go.mod h1:z4/9nQmJSSwwds7ejkxaJwO37dru3geImFUdJlaLzQo=
github.com/acomagu/bufpipe v1.0.3 h1:fxAGrHZTgQ9w5QqVItgzwj235/uYZYgbXitB+dLupOk=
Expand Down Expand Up @@ -124,8 +124,9 @@ github.com/kolo/xmlrpc v0.0.0-20190909154602-56d5ec7c422e h1:JZPIpxHmcXiQn101f6P
github.com/kolo/xmlrpc v0.0.0-20190909154602-56d5ec7c422e/go.mod h1:o03bZfuBwAXHetKXuInt4S7omeXUu62/A845kiycsSQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
Expand Down Expand Up @@ -155,11 +156,14 @@ github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLA
github.com/nsf/jsondiff v0.0.0-20200515183724-f29ed568f4ce h1:RPclfga2SEJmgMmz2k+Mg7cowZ8yv4Trqw9UsJby758=
github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw=
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/sebdah/goldie v1.0.0/go.mod h1:jXP4hmWywNEwZzhMuv2ccnqTSFpuq8iyQhtQdkkZBH4=
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
Expand Down
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
}
2 changes: 2 additions & 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 All @@ -105,6 +106,7 @@ func Provider() *schema.Provider {
"opennebula_cluster": resourceOpennebulaCluster(),
"opennebula_host": resourceOpennebulaHost(),
"opennebula_datastore": resourceOpennebulaDatastore(),
"opennebula_marketplace": resourceOpennebulaMarketPlace(),
},

ConfigureContextFunc: providerConfigure,
Expand Down
Loading

0 comments on commit 3c49a15

Please sign in to comment.