Skip to content

Commit

Permalink
Service principal fix and plugin name fixes (#7)
Browse files Browse the repository at this point in the history
* Fix for service principal auth (#7)

Signed-off-by: Viorel Dodin <[email protected]>

* Fix naming from cop to observability (#8)

Various errors were encountered during testing.
Binnary plugin name and provider name must match.

Signed-off-by: Viorel Dodin <[email protected]>

---------

Signed-off-by: Viorel Dodin <[email protected]>
  • Loading branch information
DodinViorel authored Apr 16, 2024
1 parent 6b8870c commit d413bbd
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 43 deletions.
17 changes: 9 additions & 8 deletions examples/data-sources/cop_example/data-source.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,24 @@

terraform {
required_providers {
cop = {
source = "testTerraform.com/appd/cop"
observability = {
source = "testTerraform.com/appd/observability"
}
}
}

provider "cop" {
tenant = "47a01df9-54a0-472b-96b8-7c8f64eb7cbf"
auth_method = "oauth"
url = "https://alameda-c0-test-02.saas.appd-test.com"
provider "observability" {
tenant="0eb4e853-34fb-4f77-b3fc-b9cd3b462366"
auth_method="service-principal"
url="https://aiops-dev.saas.appd-test.com"
secrets_file="/home/vdodin/aiops_secret.json"
}


data "cop_type" "ns" {
data "observability_type" "ns" {
type_name = "fmm:namespace"
}

output "myType" {
value = data.cop_type.ns
value = data.observability_type.ns
}
6 changes: 3 additions & 3 deletions examples/provider/provider_oauth.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

terraform {
required_providers {
cop = {
source = "testTerraform.com/appd/cop"
observability = {
source = "testTerraform.com/appd/observability"
}
}
}

provider "cop" {
provider "observability" {
tenant="47a01df9-54a0-472b-96b8-7c8f64eb7cbf"
auth_method="oauth"
url="https://alameda-c0-test-02.saas.appd-test.com"
Expand Down
6 changes: 3 additions & 3 deletions examples/provider/provider_service_principal.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

terraform {
required_providers {
cop = {
source = "testTerraform.com/appd/cop"
observability = {
source = "testTerraform.com/appd/observability"
}
}
}

provider "cop" {
provider "observability" {
tenant="47a01df9-54a0-472b-96b8-7c8f64eb7cbf"
auth_method="service-principal"
url="https://alameda-c0-test-02.saas.appd-test.com"
Expand Down
8 changes: 4 additions & 4 deletions examples/resources/cop_example/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@

terraform {
required_providers {
cop = {
source = "testTerraform.com/appd/cop"
observability = {
source = "testTerraform.com/appd/observability"
}
}
}

provider "cop" {
provider "observability" {
tenant = "47a01df9-54a0-472b-96b8-7c8f64eb7cbf"
auth_method = "oauth"
url = "https://alameda-c0-test-02.saas.appd-test.com"
}

resource "cop_object" "ns" {
resource "observability_object" "ns" {
type_name = "fmm:namespace"
object_id = "aws"
layer_type = "TENANT"
Expand Down
5 changes: 1 addition & 4 deletions internal/api/objectapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ func (ac *AppdClient) GetObject(fullyQualifiedTypeName, objectID, layerID, layer

req.Header.Add("Content-Type", contentType)
req.Header.Add("Accept", contentType)

if ac.AuthMethod == authMethodOAuth {
req.Header.Add("Authorization", "Bearer "+ac.Token)
}
req.Header.Add("Authorization", "Bearer "+ac.Token)

req.Header.Add("layer-id", layerID)
req.Header.Add("layer-type", layerType)
Expand Down
5 changes: 1 addition & 4 deletions internal/api/typeapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ func (ac *AppdClient) GetType(fullyQualifiedTypeName string) ([]byte, error) {

req.Header.Add("Content-Type", contentType)
req.Header.Add("Accept", contentType)

if ac.AuthMethod == authMethodOAuth {
req.Header.Add("Authorization", "Bearer "+ac.Token)
}
req.Header.Add("Authorization", "Bearer "+ac.Token)

// Do request
resp, err := ac.APIClient.Do(req)
Expand Down
32 changes: 16 additions & 16 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ type COPProviderModel struct {
}

func (p *COPProvider) Metadata(_ context.Context, _ provider.MetadataRequest, resp *provider.MetadataResponse) {
resp.TypeName = "cop"
resp.TypeName = "observability"
resp.Version = p.version
}

func (p *COPProvider) Schema(_ context.Context, _ provider.SchemaRequest, resp *provider.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"auth_method": schema.StringAttribute{
MarkdownDescription: "Authentication type selected for COP API requests. Possible values(oauth, headless, service-principal)",
MarkdownDescription: "Authentication type selected for observability API requests. Possible values(oauth, headless, service-principal)",
Required: true,
},
"tenant": schema.StringAttribute{
Expand Down Expand Up @@ -92,47 +92,47 @@ func (p *COPProvider) Configure(ctx context.Context, req provider.ConfigureReque
if data.AuthMethod.IsUnknown() {
resp.Diagnostics.AddAttributeError(
path.Root("auth_method"),
"Unknown cop API auth_method",
"Unknown observability API auth_method",
"Please make sure you configure the auth_method field",
)
}

if data.Tenant.IsUnknown() {
resp.Diagnostics.AddAttributeError(
path.Root("tenant"),
"Unknown cop API tenant",
"Unknown observability API tenant",
"Please make sure you configure the tenant field",
)
}

if data.Username.IsUnknown() {
resp.Diagnostics.AddAttributeError(
path.Root("username"),
"Unknown cop API username",
"Unknown observability API username",
"Please make sure you configure the username field",
)
}

if data.Password.IsUnknown() {
resp.Diagnostics.AddAttributeError(
path.Root("password"),
"Unknown cop API password",
"Unknown observability API password",
"Please make sure you configure the password field",
)
}

if data.URL.IsUnknown() {
resp.Diagnostics.AddAttributeError(
path.Root("url"),
"Unknown cop API url",
"Unknown observability API url",
"Please make sure you configure the url field",
)
}

if data.URL.IsUnknown() {
resp.Diagnostics.AddAttributeError(
path.Root("secrets_file"),
"Unknown cop API secrets_file",
"Unknown observability API secrets_file",
"Please make sure you configure the secrets_file field",
)
}
Expand Down Expand Up @@ -186,7 +186,7 @@ func (p *COPProvider) Configure(ctx context.Context, req provider.ConfigureReque
if authMethod == "" {
resp.Diagnostics.AddAttributeError(
path.Root("auth_method"),
"Missing cop API auth_method",
"Missing observability API auth_method",
"SET the COP_AUTH_METHOD env var or the config",
)
tflog.Error(ctx, "Missing or empty value for auth_method attribute")
Expand All @@ -197,39 +197,39 @@ func (p *COPProvider) Configure(ctx context.Context, req provider.ConfigureReque
if url == "" {
resp.Diagnostics.AddAttributeError(
path.Root("url"),
"Missing cop API url",
"Missing observability API url",
"SET the COP_URL env var or the config",
)
}

if tenantID == "" {
resp.Diagnostics.AddAttributeError(
path.Root("tenant"),
"Missing cop API tenant",
"Missing observability API tenant",
"SET the COP_TENANT env var or the config",
)
}
case "headless":
if username == "" {
resp.Diagnostics.AddAttributeError(
path.Root("username"),
"Missing cop API username",
"Missing observability API username",
"SET the COP_USERNAME env var or the config",
)
}

if password == "" {
resp.Diagnostics.AddAttributeError(
path.Root("password"),
"Missing cop API password",
"Missing observability API password",
"SET the COP_PASSWORD env var or the config",
)
}
case "service-principal":
if secretsFile == "" {
resp.Diagnostics.AddAttributeError(
path.Root("secrets_file"),
"Missing cop API secrets_file",
"Missing observability API secrets_file",
"SET the SECRETS_FILE env var or the config",
)
}
Expand All @@ -253,10 +253,10 @@ func (p *COPProvider) Configure(ctx context.Context, req provider.ConfigureReque

err := appdClient.Login()
if err != nil {
tflog.Error(ctx, fmt.Sprintf("Failed to authenticate to COP client: %s", err.Error()))
tflog.Error(ctx, fmt.Sprintf("Failed to authenticate to observability client: %s", err.Error()))
}

tflog.Debug(ctx, fmt.Sprintf("Successful authentication to COP client using %s", appdClient.AuthMethod))
tflog.Debug(ctx, fmt.Sprintf("Successful authentication to observability client using %s", appdClient.AuthMethod))

// TODO change this to a real client
resp.DataSourceData = appdClient
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func main() {
// TODO: Update this string with the published name of your provider.
// Also update the tfplugindocs generate command to either remove the
// -provider-name flag or set its value to the updated provider name.
Address: "testTerraform.com/appd/cop",
Address: "testTerraform.com/appd/observability",
Debug: debug,
}

Expand Down

0 comments on commit d413bbd

Please sign in to comment.