in internal/provider/resource_gitlab_value_stream_analytics.go [259:360]
func (r *gitlabValueStreamAnalyticsResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var data *gitlabValueStreamAnalyticsResourceModel
// Read Terraform prior state data into the model
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
fullPathType, fullPath, id, err := utils.ParseThreePartID(data.Id.ValueString())
fullPathType = strings.ToLower(fullPathType)
if err != nil {
resp.Diagnostics.AddError("Value Stream Analytics", "Errors found while parsing three part id")
return
}
if fullPathType != "group" && fullPathType != "project" {
resp.Diagnostics.AddError("Value Stream Analytics", "Error validating fullPathType, must be either 'group' or 'project'")
return
}
// read all information for refresh from resource id
query := gitlab.GraphQLQuery{
Query: fmt.Sprintf(`
query {
%s(fullPath: "%s") {
valueStreams(id: "%s") {
nodes {
id
name
namespace {
fullPath
}
stages {
id
name
custom
hidden
startEventIdentifier
startEventLabel {
id
}
endEventIdentifier
endEventLabel {
id
}
}
}
}
}
}`, fullPathType, fullPath, id),
}
tflog.Debug(ctx, "executing GraphQL Query to retrieve current value stream analytics on project", map[string]any{
"query": query.Query,
})
if fullPathType == "group" {
var response groupValueStreamResponse
if _, err := r.client.GraphQL.Do(ctx, query, &response); err != nil {
if api.Is404(err) {
tflog.Debug(ctx, "value stream analytics does not exist, removing from state", map[string]any{
"full_path": fullPath,
})
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("GitLab API error occurred", fmt.Sprintf("Unable to read value stream analytics details: %s", err.Error()))
return
}
if len(response.Data.Group.ValueStreams.Nodes) != 1 {
resp.Diagnostics.AddError("Value Stream Analytics Error", fmt.Sprintf("Expected only one value stream analytics, found %d", len(response.Data.Group.ValueStreams.Nodes)))
return
}
r.valueStreamToStateModel(&response.Data.Group.ValueStreams.Nodes[0], data, true)
} else {
var response projectValueStreamResponse
if _, err := r.client.GraphQL.Do(ctx, query, &response); err != nil {
if api.Is404(err) {
tflog.Debug(ctx, "value stream analytics does not exist, removing from state", map[string]any{
"full_path": fullPath,
})
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("GitLab API error occurred", fmt.Sprintf("Unable to read value stream analytics details: %s", err.Error()))
return
}
if len(response.Data.Project.ValueStreams.Nodes) != 1 {
resp.Diagnostics.AddError("Value Stream Analytics Error", fmt.Sprintf("Expected only one value stream analytics, found %d", len(response.Data.Project.ValueStreams.Nodes)))
return
}
r.valueStreamToStateModel(&response.Data.Project.ValueStreams.Nodes[0], data, false)
}
// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}