/
namespace.go
43 lines (36 loc) · 1.11 KB
/
namespace.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package datasource
import (
"context"
"fmt"
"github.com/hashicorp/consul/api"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/remilapeyre/terraform-provider-consul-next/internal/models"
)
func NewNamespace() datasource.DataSource {
return NewDataSource(
"namespace",
namespaceSchema(),
&Namespace{},
)
}
type Namespace struct{}
func (d *Namespace) Read(ctx context.Context, client *api.Client, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var config *api.Namespace
resp.Diagnostics.Append(models.DecodeNamespace(ctx, req.Config, &config)...)
if resp.Diagnostics.HasError() {
return
}
namespace, _, err := client.Namespaces().Read(config.Name, nil)
summary := fmt.Sprintf("failed to read namespace %q", config.Name)
if err != nil {
resp.Diagnostics.AddError(summary, err.Error())
return
}
if namespace == nil {
resp.Diagnostics.AddError(summary, fmt.Sprintf("no namespace named %q found", config.Name))
return
}
resp.Diagnostics.Append(models.Set(ctx, &resp.State, namespace)...)
}