diff --git a/internal/hubclient/client.go b/internal/hubclient/client.go index cbabe91..031bacb 100644 --- a/internal/hubclient/client.go +++ b/internal/hubclient/client.go @@ -190,3 +190,7 @@ func (c *Client) sendRequest(ctx context.Context, method string, url string, bod return nil } + +func (c *Client) Username() string { + return c.auth.Username +} diff --git a/internal/provider/data_source_login.go b/internal/provider/data_source_login.go new file mode 100644 index 0000000..6407606 --- /dev/null +++ b/internal/provider/data_source_login.go @@ -0,0 +1,94 @@ +/* + Copyright 2024 Docker Terraform Provider authors + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package provider + +import ( + "context" + "fmt" + + "github.com/docker/terraform-provider-docker/internal/hubclient" + "github.com/hashicorp/terraform-plugin-framework/datasource" + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" + "github.com/hashicorp/terraform-plugin-framework/types" +) + +var ( + _ datasource.DataSource = &LoginDataSource{} + _ datasource.DataSourceWithConfigure = &LoginDataSource{} +) + +func NewLoginDataSource() datasource.DataSource { + return &LoginDataSource{} +} + +type LoginDataSource struct { + client *hubclient.Client +} + +type LoginDataSourceModel struct { + Username types.String `tfsdk:"username"` +} + +func (d *LoginDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_login" +} + +func (d *LoginDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { + resp.Schema = schema.Schema{ + MarkdownDescription: `Reads current login info.`, + + Attributes: map[string]schema.Attribute{ + "username": schema.StringAttribute{ + MarkdownDescription: "Current username", + Computed: true, + }, + }, + } +} + +func (d *LoginDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { + // Prevent panic if the provider has not been configured. + if req.ProviderData == nil { + return + } + + client, ok := req.ProviderData.(*hubclient.Client) + + if !ok { + resp.Diagnostics.AddError( + "Unexpected Data Source Configure Type", + fmt.Sprintf("Expected *hubclient.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData), + ) + + return + } + + d.client = client +} + +func (d *LoginDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { + var data LoginDataSourceModel + resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) + + data.Username = types.StringValue(d.client.Username()) + + diags := resp.State.Set(ctx, &data) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } +} diff --git a/internal/provider/data_source_login_test.go b/internal/provider/data_source_login_test.go new file mode 100644 index 0000000..310d3e5 --- /dev/null +++ b/internal/provider/data_source_login_test.go @@ -0,0 +1,38 @@ +/* + Copyright 2024 Docker Terraform Provider authors + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package provider + +import ( + "os" + "testing" + + "github.com/hashicorp/terraform-plugin-testing/helper/resource" +) + +func TestLoginDataSource(t *testing.T) { + expected := os.Getenv("DOCKER_USERNAME") + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + { + Config: `data "docker_login" "_" {}`, + Check: resource.TestCheckResourceAttr("data.docker_login._", "username", expected), + }, + }, + }) +} diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 6688cf4..94b3a39 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -303,6 +303,7 @@ func (p *DockerProvider) DataSources(ctx context.Context) []func() datasource.Da NewAccessTokenDataSource, NewAccessTokensDataSource, NewOrgTeamDataSource, + NewLoginDataSource, } }