Skip to content

Commit

Permalink
Add serial number query option to device data source
Browse files Browse the repository at this point in the history
  • Loading branch information
danischm committed Jan 8, 2024
1 parent 8ad7f64 commit e7fe37c
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 27 deletions.
4 changes: 2 additions & 2 deletions docs/data-sources/device.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ data "catalystcenter_device" "example" {
<!-- schema generated by tfplugindocs -->
## Schema

### Required
### Optional

- `id` (String) The id of the object
- `serial_number` (String) Device serial number

### Read-Only

- `hostname` (String) Device hostname
- `pid` (String) Device product ID
- `serial_number` (String) Device serial number
- `stack` (Boolean) Device is a stacked switch
1 change: 1 addition & 0 deletions gen/definitions/device.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ attributes:
data_path: deviceInfo
type: String
mandatory: true
data_source_query: true
description: Device serial number
example: FOC12345678
- model_name: stack
Expand Down
35 changes: 23 additions & 12 deletions gen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ type YamlConfig struct {
PutIdIncludePath string `yaml:"put_id_include_path"`
PutIdQueryParam string `yaml:"put_id_query_param"`
PutNoId bool `yaml:"put_no_id"`
DataSourceNameQuery bool `yaml:"data_source_name_query"`
MinimumVersion string `yaml:"minimum_version"`
DsDescription string `yaml:"ds_description"`
ResDescription string `yaml:"res_description"`
Expand All @@ -141,6 +140,7 @@ type YamlConfigAttribute struct {
Id bool `yaml:"id"`
Reference bool `yaml:"reference"`
QueryParam bool `yaml:"query_param"`
DataSourceQuery bool `yaml:"data_source_query"`
Mandatory bool `yaml:"mandatory"`
WriteOnly bool `yaml:"write_only"`
WriteChangesOnly bool `yaml:"write_changes_only"`
Expand Down Expand Up @@ -270,19 +270,30 @@ func GetQueryParam(attributes []YamlConfigAttribute) YamlConfigAttribute {
return YamlConfigAttribute{}
}

// Templating helper function to return true if data source query attribute(s) are present
func HasDataSourceQuery(attributes []YamlConfigAttribute) bool {
for _, attr := range attributes {
if attr.DataSourceQuery {
return true
}
}
return false
}

// Map of templating functions
var functions = template.FuncMap{
"toGoName": ToGoName,
"camelCase": CamelCase,
"snakeCase": SnakeCase,
"sprintf": fmt.Sprintf,
"toLower": strings.ToLower,
"path": BuildPath,
"hasId": HasId,
"hasReference": HasReference,
"hasQueryParam": HasQueryParam,
"getId": GetId,
"getQueryParam": GetQueryParam,
"toGoName": ToGoName,
"camelCase": CamelCase,
"snakeCase": SnakeCase,
"sprintf": fmt.Sprintf,
"toLower": strings.ToLower,
"path": BuildPath,
"hasId": HasId,
"hasReference": HasReference,
"hasQueryParam": HasQueryParam,
"getId": GetId,
"getQueryParam": GetQueryParam,
"hasDataSourceQuery": HasDataSourceQuery,
}

func augmentAttribute(attr *YamlConfigAttribute) {
Expand Down
2 changes: 1 addition & 1 deletion gen/schema/schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ id_from_attribute: bool(required=False) # Set to true if the ID is derived from
put_id_include_path: str(required=False) # If PUT needs to have specific JSON path where ID should be inserted
put_id_query_param: str(required=False) # If PUT needs to have specific query parameter where ID should be inserted
put_no_id: bool(required=False) # Set to true if the PUT request does not require an ID
data_source_name_query: bool(required=False) # Set to true if the data source supports name queries
minimum_version: str(required=False) # Define a minimum supported version
ds_description: str(required=False) # Define a data source description
res_description: str(required=False) # Define a resource description
Expand All @@ -44,6 +43,7 @@ attribute:
id: bool(required=False) # Set to true if the attribute is part of the ID
reference: bool(required=False) # Set to true if the attribute is a reference being used in the path (URL) of the REST endpoint
query_param: bool(required=False) # Set to true if the attribute is a query parameter and being used for GET, POST and PUT requests
data_source_query: bool(required=False) # Set to true if the attribute is an alternative query parameter for the data source
mandatory: bool(required=False) # Set to true if the attribute is mandatory
write_only: bool(required=False) # Set to true if the attribute is write-only, meaning we cannot read the value
write_changes_only: bool(required=False) # Set to true if the attribute should only be written (included in PUT payload) if it has changed
Expand Down
30 changes: 19 additions & 11 deletions gen/templates/data_source.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 37 additions & 1 deletion internal/provider/data_source_catalystcenter_device.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e7fe37c

Please sign in to comment.