Skip to content

Commit

Permalink
Refactoring and code improvements
Browse files Browse the repository at this point in the history
- Fix commented out validity check, the terraform SDK is not good here
- Update documentation
- Refactoring and code cleanup

Mostly it's just factoring out common code blocks, but also use
a few more idiomatic constructs and reduce reparsing.
  • Loading branch information
HuwCampbell committed Feb 21, 2024
1 parent 535f96d commit 2bd89e5
Show file tree
Hide file tree
Showing 25 changed files with 503 additions and 335 deletions.
33 changes: 33 additions & 0 deletions client/resource_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,39 @@ import (
"strconv"
)

func getAnamlId(d *schema.ResourceData, key string) (int, error) {
if raw, ok := d.GetOk(key); ok {
if i, err := strconv.Atoi(raw.(string)); err == nil {
return i, nil
} else {
return 0, err
}
}
return 0, fmt.Errorf("Required Identifier %s is missing", key)
}

func getAnamlIdPointer(d *schema.ResourceData, key string) *int {
if raw, ok := d.GetOk(key); ok {
if i, err := strconv.Atoi(raw.(string)); err == nil {
return &i
} else {
return nil
}
}
return nil
}

func getStringPointer(d *schema.ResourceData, key string) *string {
if raw, ok := d.GetOk(key); ok {
if i, ok := raw.(string); ok {
return &i
} else {
return nil
}
}
return nil
}

func labelSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeString,
Expand Down
1 change: 1 addition & 0 deletions client/resource_entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func ResourceEntity() *schema.Resource {
Type: schema.TypeString,
Optional: true,
ExactlyOneOf: []string{"default_column", "entities"},
ValidateFunc: validation.StringIsNotWhiteSpace,
},
"required_type": {
Type: schema.TypeString,
Expand Down
12 changes: 6 additions & 6 deletions client/resource_entity_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ func resourceEntityMappingRead(d *schema.ResourceData, m interface{}) error {

func resourceEntityMappingCreate(d *schema.ResourceData, m interface{}) error {
c := m.(*Client)
from, _ := strconv.Atoi(d.Get("from").(string))
to, _ := strconv.Atoi(d.Get("to").(string))
feat, _ := strconv.Atoi(d.Get("mapping").(string))
from, _ := getAnamlId(d, "from")
to, _ := getAnamlId(d, "to")
feat, _ := getAnamlId(d, "mapping")

mapping := EntityMapping{
From: from,
Expand All @@ -131,9 +131,9 @@ func resourceEntityMappingCreate(d *schema.ResourceData, m interface{}) error {
func resourceEntityMappingUpdate(d *schema.ResourceData, m interface{}) error {
c := m.(*Client)
mappingID := d.Id()
from, _ := strconv.Atoi(d.Get("from").(string))
to, _ := strconv.Atoi(d.Get("to").(string))
feat, _ := strconv.Atoi(d.Get("mapping").(string))
from, _ := getAnamlId(d, "from")
to, _ := getAnamlId(d, "to")
feat, _ := getAnamlId(d, "mapping")

mapping := EntityMapping{
From: from,
Expand Down
2 changes: 1 addition & 1 deletion client/resource_entity_population.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func resourceEntityPopulationDelete(d *schema.ResourceData, m interface{}) error
}

func buildPopulation(d *schema.ResourceData) EntityPopulation {
entity, _ := strconv.Atoi(d.Get("entity").(string))
entity, _ := getAnamlId(d, "entity")
return EntityPopulation{
Name: d.Get("name").(string),
Description: d.Get("description").(string),
Expand Down
17 changes: 4 additions & 13 deletions client/resource_event_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,22 +324,13 @@ func buildEventStore(d *schema.ResourceData) (*EventStore, error) {
HasStreaming: hasStreaming,
}
}
cluster, err := strconv.Atoi(d.Get("cluster").(string))
cluster, err := getAnamlId(d, "cluster")
if err != nil {
return nil, err
}
schedule := composeNeverSchedule()
if dailySchedule, _ := expandSingleMap(d.Get("daily_schedule")); dailySchedule != nil {
schedule, err = composeDailySchedule(dailySchedule)
if err != nil {
return nil, err
}
}
if cronSchedule, _ := expandSingleMap(d.Get("cron_schedule")); cronSchedule != nil {
schedule, err = composeCronSchedule(cronSchedule)
if err != nil {
return nil, err
}
schedule, err := composeSchedule(d)
if err != nil {
return nil, err
}

entity := EventStore{
Expand Down
47 changes: 26 additions & 21 deletions client/resource_feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,19 @@ func ResourceFeature() *schema.Resource {
Optional: true,
Description: "A reference to a Table ID the feature is derived from.",
ValidateFunc: validateAnamlIdentifier(),
RequiredWith: []string{"aggregation"},
},
"select": {
Type: schema.TypeString,
Required: true,
Description: "An SQL expression for the column to aggregate.",
ValidateFunc: validation.StringIsNotWhiteSpace,
},
"filter": {
Type: schema.TypeString,
Optional: true,
Description: "An SQL column expression to filter with.",
ValidateFunc: validation.StringIsNotWhiteSpace,
},
"hours": {
Type: schema.TypeInt,
Expand Down Expand Up @@ -103,6 +106,7 @@ func ResourceFeature() *schema.Resource {
Type: schema.TypeString,
Optional: true,
Description: "An SQL expression to apply to the result of the feature aggregation.",
ValidateFunc: validation.StringIsNotWhiteSpace,
},
"entity_restrictions": {
Type: schema.TypeList,
Expand Down Expand Up @@ -317,6 +321,7 @@ func resourceFeatureDelete(d *schema.ResourceData, m interface{}) error {
}

func buildFeature(d *schema.ResourceData) (*Feature, error) {
template := getAnamlIdPointer(d, "template")
feature := Feature{
Name: d.Get("name").(string),
Description: d.Get("description").(string),
Expand All @@ -328,45 +333,41 @@ func buildFeature(d *schema.ResourceData) (*Feature, error) {
},
Labels: expandLabels(d),
Attributes: expandAttributes(d),
TemplateID: template,
}

if d.Get("template").(string) != "" {
template, _ := strconv.Atoi(d.Get("template").(string))
feature.TemplateID = &template
}

if d.Get("filter").(string) != "" {
if filter, ok := d.GetOk("filter"); ok {
feature.Filter = &SQLExpression{
SQL: d.Get("filter").(string),
SQL: filter.(string),
}
}

if d.Get("post_aggregation").(string) != "" {
if post, ok := d.GetOk("post_aggregation"); ok {
feature.PostAggExpr = &SQLExpression{
SQL: d.Get("post_aggregation").(string),
SQL: post.(string),
}
}

if d.Get("table").(string) != "" {
number, err := strconv.Atoi(d.Get("table").(string))
if table, ok := d.GetOk("table"); ok {
number, err := strconv.Atoi(table.(string))

if err != nil {
return nil, err
}

window := EventWindow{}
if d.Get("hours").(int) != 0 {
if hours, ok := d.GetOk("hours"); ok {
window.Type = "hourwindow"
window.Hours = d.Get("hours").(int)
} else if d.Get("days").(int) != 0 {
window.Hours = hours.(int)
} else if days, ok := d.GetOk("days"); ok {
window.Type = "daywindow"
window.Days = d.Get("days").(int)
} else if d.Get("months").(int) != 0 {
window.Days = days.(int)
} else if months, ok := d.GetOk("months"); ok {
window.Type = "monthwindow"
window.Months = d.Get("months").(int)
} else if d.Get("rows").(int) != 0 {
window.Months = months.(int)
} else if rows, ok := d.GetOk("rows"); ok {
window.Type = "rowwindow"
window.Rows = d.Get("rows").(int)
window.Rows = rows.(int)
} else {
window.Type = "openwindow"
}
Expand All @@ -382,10 +383,14 @@ func buildFeature(d *schema.ResourceData) (*Feature, error) {
feature.EntityRestr = nil
}
} else {
number, err := getAnamlId(d, "entity")
if err != nil {
return nil, err
}

feature.EntityID = number
feature.Type = "row"
feature.Over = expandIdentifierList(d.Get("over").([]interface{}))
number, _ := strconv.Atoi(d.Get("entity").(string))
feature.EntityID = number
}

return &feature, nil
Expand Down
5 changes: 4 additions & 1 deletion client/resource_feature_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,10 @@ func resourceFeatureSetRead(d *schema.ResourceData, m interface{}) error {

func resourceFeatureSetCreate(d *schema.ResourceData, m interface{}) error {
c := m.(*Client)
entity, _ := strconv.Atoi(d.Get("entity").(string))
entity, err := getAnamlId(d, "entity")
if err != nil {
return err
}

FeatureSet := FeatureSet{
Name: d.Get("name").(string),
Expand Down
64 changes: 12 additions & 52 deletions client/resource_feature_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,68 +314,28 @@ func resourceFeatureStoreUpdate(d *schema.ResourceData, m interface{}) error {
}

func composeFeatureStore(d *schema.ResourceData) (*FeatureStore, error) {
featureSet, err := strconv.Atoi(d.Get("feature_set").(string))
if err != nil {
return nil, err
}

var principal (*int) = nil
principalRaw, principalOk := d.GetOk("principal")
if principalOk {
principal_, err := strconv.Atoi(principalRaw.(string))
if err != nil {
return nil, err
}
principal = &principal_
}

cluster, err := strconv.Atoi(d.Get("cluster").(string))
if err != nil {
return nil, err
}

source := d.Get("additional_spark_properties").(map[string]interface{})
additionalSparkProperties := make(map[string]string)

for k, v := range source {
additionalSparkProperties[k] = v.(string)
}

var population (*int) = nil
if d.Get("entity_population").(string) != "" {
population_, err := strconv.Atoi(d.Get("entity_population").(string))
if err != nil {
return nil, err
}
population = &population_
}

var schedule = composeNeverSchedule()
if dailySchedule, _ := expandSingleMap(d.Get("daily_schedule")); dailySchedule != nil {
schedule, err = composeDailySchedule(dailySchedule)
if err != nil {
return nil, err
}
}
if cronSchedule, _ := expandSingleMap(d.Get("cron_schedule")); cronSchedule != nil {
schedule, err = composeCronSchedule(cronSchedule)
if err != nil {
return nil, err
}
featureSet, err := getAnamlId(d, "feature_set")
if err != nil {
return nil, err
}
var versionTarget (*VersionTarget) = nil
if commit, _ := d.Get("commit_target").(string); commit != "" {
versionTarget = &VersionTarget{
Type: "commit",
Commit: &commit,
}
cluster, err := getAnamlId(d, "cluster")
if err != nil {
return nil, err
}
if branch, _ := d.Get("branch_target").(string); branch != "" {
versionTarget = &VersionTarget{
Type: "branch",
Branch: &branch,
}
population := getAnamlIdPointer(d, "entity_population")
principal := getAnamlIdPointer(d, "principal")
schedule, err := composeSchedule(d)
if err != nil {
return nil, err
}
versionTarget := composeVersionTarget(d)

destinations, err := expandDestinationReferences(d.Get("destination").([]interface{}))
if err != nil {
Expand Down
Loading

0 comments on commit 2bd89e5

Please sign in to comment.