Skip to content

Commit

Permalink
Add schema.go
Browse files Browse the repository at this point in the history
  • Loading branch information
FredericBerot-Armand committed Nov 25, 2022
1 parent 0d79b19 commit 0c9a2b4
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 22 deletions.
22 changes: 0 additions & 22 deletions outscale/data_source_outscale_vms.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,6 @@ func datasourceOutscaleOApiVMS() *schema.Resource {
}
}

func dataSourceFiltersSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeSet,
Optional: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},

"values": {
Type: schema.TypeList,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
}
}

func datasourceOutscaleOApiVMSSchema() map[string]*schema.Schema {
wholeSchema := map[string]*schema.Schema{
"filter": dataSourceFiltersSchema(),
Expand Down
84 changes: 84 additions & 0 deletions outscale/schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package outscale

import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func GetDataSourcesSchema(field string, resourceSchema map[string]*schema.Schema) map[string]*schema.Schema {
result := map[string]*schema.Schema{
field: {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: setComputed(resourceSchema),
},
},
}
result = addFilter(result)
result = addRequestId(result)
return result
}

func GetDataSourceSchema(resourceSchema map[string]*schema.Schema) map[string]*schema.Schema {
result := setComputed(resourceSchema)
result = addFilter(result)
result = addRequestId(result)
return result
}

func GetResourceSchema(resourceSchema map[string]*schema.Schema) map[string]*schema.Schema {
result := addRequestId(resourceSchema)
return result
}

func addFilter(resourceSchema map[string]*schema.Schema) map[string]*schema.Schema {
resourceSchema["filter"] = dataSourceFiltersSchema()
return resourceSchema
}

func addRequestId(resourceSchema map[string]*schema.Schema) map[string]*schema.Schema {
resourceSchema["request_id"] = requestIdSchema()
return resourceSchema
}
func setComputed(resourceSchema map[string]*schema.Schema) map[string]*schema.Schema {
for k, v := range resourceSchema {
v.Computed = true
v.Required = false
v.Optional = false
v.ForceNew = false
v.Default = nil
v.DiffSuppressFunc = nil
v.ValidateFunc = nil
resourceSchema[k] = v
}
return resourceSchema
}

func requestIdSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeString,
Computed: true,
}
}

func dataSourceFiltersSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeSet,
Optional: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},

"values": {
Type: schema.TypeList,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
}
}

0 comments on commit 0c9a2b4

Please sign in to comment.