Skip to content

Commit

Permalink
Merge pull request #150 from OpsLevel/infraresaliases
Browse files Browse the repository at this point in the history
Support for aliases on  Infrastructure Resources
  • Loading branch information
taimoor ahmad authored Nov 8, 2023
2 parents 87acc28 + 078489e commit 1d3b343
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 8 deletions.
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 @@ func resourceInfrastructure() *schema.Resource {
"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 resourceInfrastructure() *schema.Resource {
}
}

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
}
// Delete
err := client.DeleteInfraAlias(existingAlias)
if err != nil {
return err
}
}
for _, expectedAlias := range expectedAliases {
if stringInArray(expectedAlias, existingAliases) {
continue
}
// Add
id := opslevel.NewID(resource.Id)
_, err := client.CreateAliases(*id, []string{expectedAlias})
if err != nil {
return err
}
}
return nil
}

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

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

return resourceInfrastructureRead(d, client)
}

Expand Down Expand Up @@ -147,7 +181,7 @@ func resourceInfrastructureRead(d *schema.ResourceData, client *opslevel.Client)
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{
Schema: d.Get("schema").(string),
Owner: opslevel.NewID(d.Get("owner").(string)),
Provider: expandInfraProviderData(d),
Expand All @@ -157,6 +191,13 @@ func resourceInfrastructureUpdate(d *schema.ResourceData, client *opslevel.Clien
return err
}

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

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 @@ func resourceServiceCreate(d *schema.ResourceData, client *opslevel.Client) erro
}
_, 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])
}
}

Expand Down Expand Up @@ -338,9 +338,9 @@ func resourceServiceUpdate(d *schema.ResourceData, client *opslevel.Client) erro
}

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

Expand All @@ -367,7 +367,7 @@ func resourceServiceUpdate(d *schema.ResourceData, client *opslevel.Client) erro
}
_, 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])
}
}

Expand Down

0 comments on commit 1d3b343

Please sign in to comment.