Skip to content

Commit

Permalink
update data source
Browse files Browse the repository at this point in the history
Signed-off-by: ziyeqf <51212351+ziyeqf@users.noreply.github.com>
  • Loading branch information
ziyeqf committed Nov 1, 2023
1 parent 6a99ee5 commit 74d1b5d
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions internal/services/authorization/role_definition_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,6 @@ func (a ArmRoleDefinitionDataSource) Read() sdk.ResourceFunc {
}

defId := config.RoleDefinitionId
scope := config.Scope

if scope == "" {
scope = commonids.NewSubscriptionID(metadata.Client.Account.SubscriptionId).ID()
}

// search by name
var id roledefinitions.ScopedRoleDefinitionId
Expand All @@ -169,7 +164,7 @@ func (a ArmRoleDefinitionDataSource) Read() sdk.ResourceFunc {
return fmt.Errorf("internal error: context had no deadline")
}
err := pluginsdk.Retry(time.Until(deadline), func() *pluginsdk.RetryError {
roleDefinitions, err := client.List(ctx, commonids.NewScopeID(scope), roledefinitions.ListOperationOptions{
roleDefinitions, err := client.List(ctx, commonids.NewScopeID(config.Scope), roledefinitions.ListOperationOptions{
Filter: pointer.To(fmt.Sprintf("roleName eq '%s'", config.Name)),
})
if err != nil {
Expand All @@ -185,14 +180,14 @@ func (a ArmRoleDefinitionDataSource) Read() sdk.ResourceFunc {
return pluginsdk.NonRetryableError(fmt.Errorf("loading Role Definition List: values[0].ID is nil '%s'", config.Name))
}

id = roledefinitions.NewScopedRoleDefinitionID(scope, *(*roleDefinitions.Model)[0].Name)
id = roledefinitions.NewScopedRoleDefinitionID(config.Scope, *(*roleDefinitions.Model)[0].Name)
return nil
})
if err != nil {
return err
}
} else {
id = roledefinitions.NewScopedRoleDefinitionID(scope, defId)
id = roledefinitions.NewScopedRoleDefinitionID(config.Scope, defId)
}

resp, err := client.Get(ctx, id)
Expand All @@ -219,7 +214,9 @@ func (a ArmRoleDefinitionDataSource) Read() sdk.ResourceFunc {
state.AssignableScopes = pointer.From(props.AssignableScopes)
}

metadata.SetID(id)
// when `config.Scope` is "", the id starts with two "/"
// so we take the id from response to keep consistency
metadata.ResourceData.SetId(*role.Id)
return metadata.Encode(&state)
},
}
Expand Down

0 comments on commit 74d1b5d

Please sign in to comment.