Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Terraform generate: check if email provider exists #857

Merged
merged 3 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/auth/scopes.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var RequiredScopes = []string{
"create:users", "delete:users", "read:users", "update:users",
"read:branding", "update:branding",
"read:email_templates", "update:email_templates",
"read:email_provider",
"read:connections", "update:connections",
"read:client_keys", "read:logs", "read:tenant_settings",
"read:custom_domains", "create:custom_domains", "update:custom_domains", "delete:custom_domains",
Expand Down
2 changes: 2 additions & 0 deletions internal/auth0/auth0.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type API struct {
Connection ConnectionAPI
CustomDomain CustomDomainAPI
EmailTemplate EmailTemplateAPI
EmailProvider EmailProviderAPI
Log LogAPI
LogStream LogStreamAPI
Organization OrganizationAPI
Expand All @@ -42,6 +43,7 @@ func NewAPI(m *management.Management) *API {
Connection: m.Connection,
CustomDomain: m.CustomDomain,
EmailTemplate: m.EmailTemplate,
EmailProvider: m.EmailProvider,
Log: m.Log,
LogStream: m.LogStream,
Organization: m.Organization,
Expand Down
13 changes: 13 additions & 0 deletions internal/auth0/email_provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package auth0

import (
"context"

"github.com/auth0/go-auth0/management"
)

type EmailProviderAPI interface {
// Read email provider details.
// See: https://auth0.com/docs/api/management/v2#!/Emails/get_provider
Read(ctx context.Context, opts ...management.RequestOption) (ep *management.EmailProvider, err error)
}
56 changes: 56 additions & 0 deletions internal/auth0/mock/email_provider_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/cli/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (i *terraformInputs) parseResourceFetchers(api *auth0.API) ([]resourceDataF
case "auth0_custom_domain":
fetchers = append(fetchers, &customDomainResourceFetcher{api})
case "auth0_email_provider":
fetchers = append(fetchers, &emailProviderResourceFetcher{})
fetchers = append(fetchers, &emailProviderResourceFetcher{api})
case "auth0_guardian":
fetchers = append(fetchers, &guardianResourceFetcher{})
case "auth0_log_stream":
Expand Down
15 changes: 13 additions & 2 deletions internal/cli/terraform_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli

import (
"context"
"net/http"
"strings"

"github.com/auth0/go-auth0/management"
Expand Down Expand Up @@ -49,7 +50,9 @@ type (
api *auth0.API
}

emailProviderResourceFetcher struct{}
emailProviderResourceFetcher struct {
api *auth0.API
}

guardianResourceFetcher struct{}
logStreamResourceFetcher struct {
Expand Down Expand Up @@ -220,7 +223,15 @@ func (f *customDomainResourceFetcher) FetchData(ctx context.Context) (importData
return data, nil
}

func (f *emailProviderResourceFetcher) FetchData(_ context.Context) (importDataList, error) {
func (f *emailProviderResourceFetcher) FetchData(ctx context.Context) (importDataList, error) {
_, err := f.api.EmailProvider.Read(ctx)
if err != nil {
if mErr, ok := err.(management.Error); ok && mErr.Status() == http.StatusNotFound {
return nil, nil
}
return nil, err
}

return []importDataItem{
{
ResourceName: "auth0_email_provider.email_provider",
Expand Down
56 changes: 55 additions & 1 deletion internal/cli/terraform_fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cli
import (
"context"
"fmt"
"net/http"
"strings"
"testing"

Expand Down Expand Up @@ -560,14 +561,67 @@ func TestGuardianResourceFetcher_FetchData(t *testing.T) {

func TestEmailProviderResourceFetcher_FetchData(t *testing.T) {
t.Run("it successfully generates email provider import data", func(t *testing.T) {
fetcher := emailProviderResourceFetcher{}
ctrl := gomock.NewController(t)
defer ctrl.Finish()
emailProviderAPI := mock.NewMockEmailProviderAPI(ctrl)

emailProviderAPI.EXPECT().
Read(gomock.Any(), gomock.Any()).
Return(nil, nil)

fetcher := emailProviderResourceFetcher{
api: &auth0.API{
EmailProvider: emailProviderAPI,
},
}

data, err := fetcher.FetchData(context.Background())
assert.NoError(t, err)
assert.Len(t, data, 1)
assert.Equal(t, data[0].ResourceName, "auth0_email_provider.email_provider")
assert.Greater(t, len(data[0].ImportID), 0)
})

t.Run("it does not generate email provider import data if email provider does not exist", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mErr := mockManagamentError{status: http.StatusNotFound}

emailProviderAPI := mock.NewMockEmailProviderAPI(ctrl)
emailProviderAPI.EXPECT().
Read(gomock.Any(), gomock.Any()).
Return(nil, mErr)

fetcher := emailProviderResourceFetcher{
api: &auth0.API{
EmailProvider: emailProviderAPI,
},
}

data, err := fetcher.FetchData(context.Background())
assert.NoError(t, err)
assert.Len(t, data, 0)
})

t.Run("it returns an error if api call fails", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

emailProviderAPI := mock.NewMockEmailProviderAPI(ctrl)
emailProviderAPI.EXPECT().
Read(gomock.Any()).
Return(nil, fmt.Errorf("failed to read email provider"))

fetcher := emailProviderResourceFetcher{
api: &auth0.API{
EmailProvider: emailProviderAPI,
},
}

_, err := fetcher.FetchData(context.Background())
assert.EqualError(t, err, "failed to read email provider")
})
}

func TestLogStreamResourceFetcher_FetchData(t *testing.T) {
Expand Down
Loading