Skip to content

Commit

Permalink
Merge pull request #193 from lindgrenj6/bulk-create-db-fixes
Browse files Browse the repository at this point in the history
Change BulkCreate ApplicationAuthentication logic to work with DB-backed DAO
  • Loading branch information
lpichler committed Apr 1, 2022
2 parents 283dd25 + 28c196d commit a700bdf
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 53 deletions.
9 changes: 5 additions & 4 deletions application_authentication_handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"net/http"
"strconv"
"testing"

"github.com/RedHatInsights/sources-api-go/internal/testutils"
Expand Down Expand Up @@ -67,8 +68,8 @@ func TestApplicationAuthenticationList(t *testing.T) {
t.Error("ghosts infected the return")
}

authID := fixtures.TestAuthenticationData[0].ID
if appAuth["authentication_uid"] != authID {
authID := strconv.Itoa(int(fixtures.TestAuthenticationData[0].DbID))
if appAuth["authentication_id"].(string) != authID {
t.Error("ghosts infected the return")
}

Expand Down Expand Up @@ -128,8 +129,8 @@ func TestApplicationAuthenticationGet(t *testing.T) {
if err != nil {
t.Error("Failed unmarshaling output")
}
authID := fixtures.TestAuthenticationData[0].ID
if out.AuthenticationUID != authID {
authID := strconv.Itoa(int(fixtures.TestAuthenticationData[0].DbID))
if out.AuthenticationID != authID {
t.Error("ghosts infected the return")
}

Expand Down
9 changes: 0 additions & 9 deletions dao/application_authentication_dao_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,6 @@ func TestDeleteApplicationAuthentication(t *testing.T) {
}
}

{
want := applicationAuthentication.AuthenticationUID
got := deletedApplicationAuthentication.AuthenticationUID

if want != got {
t.Errorf(`incorrect applicationAuthentication deleted. Want "%s" in the authenticationUid field, got "%s"`, want, got)
}
}

DoneWithFixtures("delete")
}

Expand Down
37 changes: 18 additions & 19 deletions model/application_authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"
"time"

"github.com/RedHatInsights/sources-api-go/config"
"github.com/RedHatInsights/sources-api-go/util"
)

Expand All @@ -14,30 +15,29 @@ type ApplicationAuthentication struct {
UpdatedAt time.Time `json:"updated_at"`
PausedAt *time.Time `json:"paused_at"`

VaultPath string `json:"vault_path"`
VaultPath string `json:"vault_path" gorm:"-"`

TenantID int64
Tenant Tenant

ApplicationID int64 `json:"application_id"`
Application Application
// TODO: fix correctly PR#40
AuthenticationID int64 `json:"authentication_id"`
AuthenticationID int64 `json:"-"`

AuthenticationUID string `json:"-"`
AuthenticationUID string `json:"-" gorm:"-"`
}

func (aa *ApplicationAuthentication) ToEvent() interface{} {
aaEvent := &ApplicationAuthenticationEvent{
ID: aa.ID,
PausedAt: util.DateTimePointerToRecordFormat(aa.PausedAt),
CreatedAt: util.DateTimeToRecordFormat(aa.CreatedAt),
UpdatedAt: util.DateTimeToRecordFormat(aa.UpdatedAt),
ApplicationID: aa.ApplicationID,
AuthenticationID: aa.AuthenticationID,
AuthenticationUID: aa.AuthenticationUID,
Tenant: &aa.Tenant.ExternalTenant,
VaultPath: aa.VaultPath,
ID: aa.ID,
PausedAt: util.DateTimePointerToRecordFormat(aa.PausedAt),
CreatedAt: util.DateTimeToRecordFormat(aa.CreatedAt),
UpdatedAt: util.DateTimeToRecordFormat(aa.UpdatedAt),
ApplicationID: aa.ApplicationID,
AuthenticationID: aa.AuthenticationID,
Tenant: &aa.Tenant.ExternalTenant,
VaultPath: aa.VaultPath,
}

return aaEvent
Expand All @@ -47,19 +47,18 @@ func (aa *ApplicationAuthentication) ToResponse() *ApplicationAuthenticationResp
id := strconv.FormatInt(aa.ID, 10)
appId := strconv.FormatInt(aa.ApplicationID, 10)
authId := ""
if aa.VaultPath != "" {
if aa.VaultPath != "" && config.IsVaultOn() {
parts := strings.Split(aa.VaultPath, "/")
authId = parts[len(parts)-1]
} else {
authId = strconv.FormatInt(aa.AuthenticationID, 10)
}

return &ApplicationAuthenticationResponse{
ID: id,
AuthenticationUID: aa.AuthenticationUID,
CreatedAt: util.DateTimeToRFC3339(aa.CreatedAt),
UpdatedAt: util.DateTimeToRFC3339(aa.UpdatedAt),
ApplicationID: appId,
AuthenticationID: authId,
ID: id,
CreatedAt: util.DateTimeToRFC3339(aa.CreatedAt),
UpdatedAt: util.DateTimeToRFC3339(aa.UpdatedAt),
ApplicationID: appId,
AuthenticationID: authId,
}
}
12 changes: 7 additions & 5 deletions model/authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ import (
type Authentication struct {
DbID int64 `gorm:"primaryKey; column:id" json:"-"`
ID string `json:"id"`
CreatedAt time.Time `json:"created_at"`
CreatedAt time.Time `json:"created_at" gorm:"-"`

Name string `json:"name,omitempty"`
AuthType string `gorm:"column:authtype" json:"authtype"`
Username string `json:"username"`
Password string `json:"password"`
Extra map[string]interface{} `gorm:"-" json:"extra,omitempty"`
ExtraDb datatypes.JSON `gorm:"column:extra"`
Version string `json:"version"`
Version string `json:"version" gorm:"-"`

AvailabilityStatus string `json:"availability_status,omitempty"`
LastCheckedAt *time.Time `json:"last_checked_at,omitempty"`
Expand Down Expand Up @@ -176,9 +176,11 @@ func mapIdExtraFields(auth *Authentication) (string, map[string]interface{}) {
} else {
id = strconv.FormatInt(auth.DbID, 10)

err := json.Unmarshal(auth.ExtraDb, &extra)
if err != nil {
logger.Log.Errorf(`could not unmarshal "extra" field from authentication with ID "%s"`, id)
if auth.ExtraDb != nil {
err := json.Unmarshal(auth.ExtraDb, &extra)
if err != nil {
logger.Log.Errorf(`could not unmarshal "extra" field from authentication with ID "%s"`, id)
}
}
}

Expand Down
14 changes: 8 additions & 6 deletions model/event_models.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,14 @@ type ApplicationAuthenticationEvent struct {
UpdatedAt *string `json:"updated_at"`
PausedAt *string `json:"paused_at"`

ApplicationID int64 `json:"application_id"`
AuthenticationID int64 `json:"authentication_id"`
AuthenticationUID string `json:"authentication_uid"`

Tenant *string `json:"tenant"`
VaultPath string `json:"vault_path"`
ApplicationID int64 `json:"application_id"`
AuthenticationID int64 `json:"authentication_id"`
// TODO: maybe add back in if we get vault
AuthenticationUID string `json:"-"`

Tenant *string `json:"tenant"`
// TODO: add back in if we get vault
VaultPath string `json:"-"`
}

type EndpointEvent struct {
Expand Down
8 changes: 4 additions & 4 deletions service/bulk_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ func BulkAssembly(req m.BulkCreateRequest, tenantID *int64) (*m.BulkCreateOutput
return err
}

if strings.ToLower(output.Authentications[i].ResourceType) == "Application" {
if strings.ToLower(output.Authentications[i].ResourceType) == "application" {
output.ApplicationAuthentications = append(output.ApplicationAuthentications, m.ApplicationAuthentication{
// TODO: After vault migration.
// VaultPath: output.Authentications[i].Path(),
ApplicationID: output.Authentications[i].ResourceID,
AuthenticationUID: output.Authentications[i].ID,
TenantID: *tenantID,
ApplicationID: output.Authentications[i].ResourceID,
AuthenticationID: output.Authentications[i].DbID,
TenantID: *tenantID,
})
}
}
Expand Down
4 changes: 1 addition & 3 deletions statuslistener/test_data/bulk_message_Application_1.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
{
"application_id": 1,
"authentication_id": 1,
"authentication_uid": "611a8a38-f434-4e62-bda0-78cd45ffae5b",
"created_at": "2022-01-19 11:57:23 CET",
"id": 1,
"paused_at": null,
"tenant": "12345",
"updated_at": "2022-01-19 11:57:23 CET",
"vault_path": "Application_1_611a8a38-f434-4e62-bda0-78cd45ffae5b"
"updated_at": "2022-01-19 11:57:23 CET"
}
],
"applications": [
Expand Down
4 changes: 1 addition & 3 deletions statuslistener/test_data/bulk_message_Source_1.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
{
"application_id": 1,
"authentication_id": 1,
"authentication_uid": "611a8a38-f434-4e62-bda0-78cd45ffae5b",
"created_at": "2022-01-19 11:57:23 CET",
"id": 1,
"paused_at": null,
"tenant": "12345",
"updated_at": "2022-01-19 11:57:23 CET",
"vault_path": "Application_1_611a8a38-f434-4e62-bda0-78cd45ffae5b"
"updated_at": "2022-01-19 11:57:23 CET"
}
],
"applications": [
Expand Down

0 comments on commit a700bdf

Please sign in to comment.