Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for aliases on Infrastructure Resources #150

Merged
6 commits merged into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changes/unreleased/Bugfix-20231107-162955.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kind: Bugfix
body: Fix index out of range when returning Service errors
time: 2023-11-07T16:29:55.13703-05:00
3 changes: 3 additions & 0 deletions .changes/unreleased/Feature-20231107-163024.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kind: Feature
body: Add support for managing aliases on Infrastructure Resources
time: 2023-11-07T16:30:24.334158-05:00
45 changes: 43 additions & 2 deletions opslevel/resource_opslevel_infra.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"aliases": {
Type: schema.TypeList,
Description: "The aliases of the infrastructure resource.",
Computed: true,
ForceNew: false,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"schema": {
Expand Down Expand Up @@ -80,6 +81,33 @@
}
}

func reconcileInfraAliases(d *schema.ResourceData, resource *opslevel.InfrastructureResource, client *opslevel.Client) error {
expectedAliases := getStringArray(d, "aliases")
existingAliases := resource.Aliases
for _, existingAlias := range existingAliases {
if stringInArray(existingAlias, expectedAliases) {
continue

Check warning on line 89 in opslevel/resource_opslevel_infra.go

View check run for this annotation

Codecov / codecov/patch

opslevel/resource_opslevel_infra.go#L84-L89

Added lines #L84 - L89 were not covered by tests
}
// Delete
err := client.DeleteInfraAlias(existingAlias)
if err != nil {
return err

Check warning on line 94 in opslevel/resource_opslevel_infra.go

View check run for this annotation

Codecov / codecov/patch

opslevel/resource_opslevel_infra.go#L92-L94

Added lines #L92 - L94 were not covered by tests
}
}
for _, expectedAlias := range expectedAliases {
if stringInArray(expectedAlias, existingAliases) {
continue

Check warning on line 99 in opslevel/resource_opslevel_infra.go

View check run for this annotation

Codecov / codecov/patch

opslevel/resource_opslevel_infra.go#L97-L99

Added lines #L97 - L99 were not covered by tests
}
// Add
id := opslevel.NewID(resource.Id)
_, err := client.CreateAliases(*id, []string{expectedAlias})
if err != nil {
return err

Check warning on line 105 in opslevel/resource_opslevel_infra.go

View check run for this annotation

Codecov / codecov/patch

opslevel/resource_opslevel_infra.go#L102-L105

Added lines #L102 - L105 were not covered by tests
}
}
return nil

Check warning on line 108 in opslevel/resource_opslevel_infra.go

View check run for this annotation

Codecov / codecov/patch

opslevel/resource_opslevel_infra.go#L108

Added line #L108 was not covered by tests
}

func flattenInfraProviderData(resource *opslevel.InfrastructureResource) []map[string]any {
return []map[string]any{{
"account": resource.ProviderData.AccountName,
Expand Down Expand Up @@ -114,6 +142,12 @@
return err
}
d.SetId(resource.Id)

err = reconcileInfraAliases(d, resource, client)
if err != nil {
return err

Check warning on line 148 in opslevel/resource_opslevel_infra.go

View check run for this annotation

Codecov / codecov/patch

opslevel/resource_opslevel_infra.go#L146-L148

Added lines #L146 - L148 were not covered by tests
}

return resourceInfrastructureRead(d, client)
}

Expand Down Expand Up @@ -147,7 +181,7 @@
func resourceInfrastructureUpdate(d *schema.ResourceData, client *opslevel.Client) error {
id := d.Id()

_, err := client.UpdateInfrastructure(id, opslevel.InfraInput{
resource, err := client.UpdateInfrastructure(id, opslevel.InfraInput{

Check warning on line 184 in opslevel/resource_opslevel_infra.go

View check run for this annotation

Codecov / codecov/patch

opslevel/resource_opslevel_infra.go#L184

Added line #L184 was not covered by tests
Schema: d.Get("schema").(string),
Owner: opslevel.NewID(d.Get("owner").(string)),
Provider: expandInfraProviderData(d),
Expand All @@ -157,6 +191,13 @@
return err
}

if d.HasChange("aliases") {
err = reconcileInfraAliases(d, resource, client)
if err != nil {
return err

Check warning on line 197 in opslevel/resource_opslevel_infra.go

View check run for this annotation

Codecov / codecov/patch

opslevel/resource_opslevel_infra.go#L194-L197

Added lines #L194 - L197 were not covered by tests
}
}

d.Set("last_updated", timeLastUpdated())
return resourceInfrastructureRead(d, client)
}
Expand Down
10 changes: 5 additions & 5 deletions opslevel/resource_opslevel_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@
}
_, err := client.ServiceApiDocSettingsUpdate(string(resource.Id), docPath.(string), source)
if err != nil {
log.Error().Err(err).Msgf("failed to update service '%s' api doc settings", resource.ManagedAliases[0])
log.Error().Err(err).Msgf("failed to update service '%s' api doc settings", resource.Aliases[0])

Check warning on line 237 in opslevel/resource_opslevel_service.go

View check run for this annotation

Codecov / codecov/patch

opslevel/resource_opslevel_service.go#L237

Added line #L237 was not covered by tests
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noticed that this is a bug that was introduced in services. If the user has no set aliases, this line will crash instead of erroring. Aliases[0] is being used because it's guaranteed that the slug/friendly_id will be present there.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

}
}

Expand Down Expand Up @@ -338,9 +338,9 @@
}

if d.HasChange("aliases") {
tagsErr := reconcileServiceAliases(d, resource, client)
if tagsErr != nil {
return tagsErr
err = reconcileServiceAliases(d, resource, client)
if err != nil {
return err

Check warning on line 343 in opslevel/resource_opslevel_service.go

View check run for this annotation

Codecov / codecov/patch

opslevel/resource_opslevel_service.go#L341-L343

Added lines #L341 - L343 were not covered by tests
}
}

Expand All @@ -367,7 +367,7 @@
}
_, err := client.ServiceApiDocSettingsUpdate(string(resource.Id), docPath, docSource)
if err != nil {
log.Error().Err(err).Msgf("failed to update service '%s' api doc settings", resource.ManagedAliases[0])
log.Error().Err(err).Msgf("failed to update service '%s' api doc settings", resource.Aliases[0])

Check warning on line 370 in opslevel/resource_opslevel_service.go

View check run for this annotation

Codecov / codecov/patch

opslevel/resource_opslevel_service.go#L370

Added line #L370 was not covered by tests
}
}

Expand Down
Loading