Skip to content

Commit

Permalink
RHINENG-9639 remove unwanted fields from API response (#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
r14chandra authored May 2, 2024
1 parent d7ed806 commit 757d0be
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 79 deletions.
6 changes: 3 additions & 3 deletions internal/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

const (
fields = `profile_id, name, label, account_id, org_id, timezone('UTC', created_at) AS created_at, active, creator, insights, remediations, compliance`
fields = `profile_id, account_id, org_id, timezone('UTC', created_at) AS created_at, active, insights, remediations, compliance`
)

var (
Expand Down Expand Up @@ -79,12 +79,12 @@ func Handle() *sql.DB {

// InsertProfile creates a new record in the profiles table from profile.
func InsertProfile(profile Profile) error {
stmt, err := preparedStatement(`INSERT INTO profiles (profile_id, name, label, account_id, org_id, insights, remediations, compliance, active, creator) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10);`)
stmt, err := preparedStatement(`INSERT INTO profiles (profile_id, account_id, org_id, insights, remediations, compliance, active) VALUES ($1, $2, $3, $4, $5, $6, $7);`)
if err != nil {
return fmt.Errorf("cannot prepare INSERT: %w", err)
}

_, err = stmt.Exec(profile.ID, profile.Name, profile.Label, profile.AccountID, profile.OrgID, profile.Insights, profile.Remediations, profile.Compliance, profile.Active, profile.Creator)
_, err = stmt.Exec(profile.ID, profile.AccountID, profile.OrgID, profile.Insights, profile.Remediations, profile.Compliance, profile.Active)
if err != nil {
return fmt.Errorf("cannot execute INSERT: %w", err)
}
Expand Down
18 changes: 1 addition & 17 deletions internal/db/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@ import (

type Profile struct {
ID uuid.UUID `json:"id" db:"profile_id"`
Name *JSONNullString `json:"name,omitempty" db:"name"`
Label *JSONNullString `json:"label,omitempty" db:"label"`
AccountID *JSONNullString `json:"account_id,omitempty" db:"account_id"`
OrgID *JSONNullString `json:"org_id,omitempty" db:"org_id"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
Active bool `json:"active" db:"active"`
Creator *JSONNullString `json:"creator,omitempty" db:"creator"`
Insights bool `json:"insights" db:"insights"`
Remediations bool `json:"remediations" db:"remediations"`
Compliance bool `json:"compliance" db:"compliance"`
Expand All @@ -26,10 +23,8 @@ type Profile struct {
func NewProfile(orgID string, accountID string, state map[string]string) *Profile {
profile := Profile{
ID: uuid.New(),
Label: &JSONNullString{NullString: sql.NullString{Valid: true, String: accountID + "-" + uuid.New().String()}},
AccountID: &JSONNullString{NullString: sql.NullString{Valid: accountID != "", String: accountID}},
OrgID: &JSONNullString{NullString: sql.NullString{Valid: orgID != "", String: orgID}},
Creator: &JSONNullString{NullString: sql.NullString{Valid: true, String: "redhat"}},
CreatedAt: time.Now(),
Active: true,
}
Expand All @@ -42,22 +37,11 @@ func NewProfile(orgID string, accountID string, state map[string]string) *Profil
// where appropriate.
func CopyProfile(from Profile) Profile {
return Profile{
ID: uuid.New(),
Name: from.Name,
Label: func() *JSONNullString {
val := JSONNullStringSafeValue(from.AccountID) + "-" + uuid.New().String()
return &JSONNullString{
NullString: sql.NullString{
Valid: true,
String: val,
},
}
}(),
ID: uuid.New(),
AccountID: from.AccountID,
OrgID: from.OrgID,
CreatedAt: time.Now(),
Active: from.Active,
Creator: from.Creator,
Insights: from.Insights,
Remediations: from.Remediations,
Compliance: from.Compliance,
Expand Down
10 changes: 2 additions & 8 deletions internal/db/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,15 @@ func TestProfileMarshalJSON(t *testing.T) {
{
input: Profile{
ID: uuid.MustParse("a863569d-6e57-4082-ba80-20e0089738ff"),
Name: &JSONNullString{NullString: sql.NullString{Valid: true, String: "default"}},
Label: &JSONNullString{NullString: sql.NullString{Valid: true, String: "91fb2f8b"}},
AccountID: &JSONNullString{NullString: sql.NullString{Valid: true, String: "123456"}},
OrgID: &JSONNullString{NullString: sql.NullString{Valid: true, String: "654321"}},
CreatedAt: time.Unix(0, 0).UTC(),
Active: true,
Creator: &JSONNullString{NullString: sql.NullString{Valid: true, String: "root"}},
Insights: true,
Remediations: true,
Compliance: true,
},
want: []byte(`{"id":"a863569d-6e57-4082-ba80-20e0089738ff","name":"default","label":"91fb2f8b","account_id":"123456","org_id":"654321","created_at":"1970-01-01T00:00:00Z","active":true,"creator":"root","insights":true,"remediations":true,"compliance":true}`),
want: []byte(`{"id":"a863569d-6e57-4082-ba80-20e0089738ff","account_id":"123456","org_id":"654321","created_at":"1970-01-01T00:00:00Z","active":true,"insights":true,"remediations":true,"compliance":true}`),
},
}

Expand Down Expand Up @@ -64,16 +61,13 @@ func TestProfileUnmarshalJSON(t *testing.T) {
wantError error
}{
{
input: []byte(`{"id":"a863569d-6e57-4082-ba80-20e0089738ff","name":"default","label":"91fb2f8b","account_id":"123456","org_id":"654321","created_at":"1970-01-01T00:00:00Z","active":true,"creator":"root","insights":true,"remediations":true,"compliance":true}`),
input: []byte(`{"id":"a863569d-6e57-4082-ba80-20e0089738ff","account_id":"123456","org_id":"654321","created_at":"1970-01-01T00:00:00Z","active":true,"insights":true,"remediations":true,"compliance":true}`),
want: Profile{
ID: uuid.MustParse("a863569d-6e57-4082-ba80-20e0089738ff"),
Name: &JSONNullString{NullString: sql.NullString{Valid: true, String: "default"}},
Label: &JSONNullString{NullString: sql.NullString{Valid: true, String: "91fb2f8b"}},
AccountID: &JSONNullString{NullString: sql.NullString{Valid: true, String: "123456"}},
OrgID: &JSONNullString{NullString: sql.NullString{Valid: true, String: "654321"}},
CreatedAt: time.Unix(0, 0).UTC(),
Active: true,
Creator: &JSONNullString{NullString: sql.NullString{Valid: true, String: "root"}},
Insights: true,
Remediations: true,
Compliance: true,
Expand Down
10 changes: 0 additions & 10 deletions internal/http/v1/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ type accountState struct {
Account string `json:"account"`
State map[string]string `json:"state"`
ID string `json:"id"`
Label string `json:"label"`
ApplyState bool `json:"apply_state"`
OrgID string `json:"org_id"`
}
Expand All @@ -34,8 +33,6 @@ type accountState struct {
type stateArchive struct {
Account string `json:"account"`
ID string `json:"id"`
Label string `json:"label"`
Initiator string `json:"initiator"`
CreatedAt time.Time `json:"created_at"`
State map[string]string `json:"state"`
OrgID string `json:"org_id"`
Expand Down Expand Up @@ -90,7 +87,6 @@ func postStates(w http.ResponseWriter, r *http.Request) {
Account: db.JSONNullStringSafeValue(currentProfile.AccountID),
ApplyState: currentProfile.Active,
ID: currentProfile.ID.String(),
Label: currentProfile.Label.String,
OrgID: currentProfile.OrgID.String,
State: currentProfile.StateConfig(),
}
Expand Down Expand Up @@ -145,7 +141,6 @@ func postStates(w http.ResponseWriter, r *http.Request) {
Account: db.JSONNullStringSafeValue(newProfile.AccountID),
ApplyState: newProfile.Active,
ID: newProfile.ID.String(),
Label: newProfile.Label.String,
OrgID: newProfile.OrgID.String,
State: newProfile.StateConfig(),
}
Expand Down Expand Up @@ -211,8 +206,6 @@ func getStates(w http.ResponseWriter, r *http.Request) {
s := stateArchive{
Account: db.JSONNullStringSafeValue(profile.AccountID),
ID: profile.ID.String(),
Label: db.JSONNullStringSafeValue(profile.Label),
Initiator: db.JSONNullStringSafeValue(profile.Creator),
CreatedAt: profile.CreatedAt.UTC(),
State: make(map[string]string),
OrgID: db.JSONNullStringSafeValue(profile.OrgID),
Expand Down Expand Up @@ -272,7 +265,6 @@ func getCurrentState(w http.ResponseWriter, r *http.Request) {
Account: db.JSONNullStringSafeValue(profile.AccountID),
State: profile.StateConfig(),
ID: profile.ID.String(),
Label: db.JSONNullStringSafeValue(profile.Label),
ApplyState: profile.Active,
OrgID: db.JSONNullStringSafeValue(profile.OrgID),
}
Expand Down Expand Up @@ -308,8 +300,6 @@ func getStateByID(w http.ResponseWriter, r *http.Request) {
resp := stateArchive{
Account: db.JSONNullStringSafeValue(profile.AccountID),
ID: profile.ID.String(),
Label: db.JSONNullStringSafeValue(profile.Label),
Initiator: db.JSONNullStringSafeValue(profile.Creator),
CreatedAt: profile.CreatedAt.UTC(),
State: profile.StateConfig(),
OrgID: db.JSONNullStringSafeValue(profile.OrgID),
Expand Down
6 changes: 3 additions & 3 deletions internal/http/v1/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func TestGetStates(t *testing.T) {
},
want: response{
code: http.StatusOK,
body: []byte(`{"count":2,"limit":0,"offset":0,"total":2,"results":[{"account":"10064","id":"b5db9cbc-4ecd-464b-b416-3a6cd67af87a","label":"","initiator":"","created_at":"1970-01-01T00:00:00Z","state":{"compliance_openscap":"disabled","insights":"disabled","remediations":"disabled"},"org_id":"78606"},{"account":"10064","id":"3c8859ae-ef4e-4136-ab17-ccd4ea9f36bf","label":"","initiator":"","created_at":"1970-01-01T00:00:00Z","state":{"compliance_openscap":"enabled","insights":"enabled","remediations":"enabled"},"org_id":"78606"}]}`),
body: []byte(`{"count":2,"limit":0,"offset":0,"total":2,"results":[{"account":"10064","id":"b5db9cbc-4ecd-464b-b416-3a6cd67af87a","created_at":"1970-01-01T00:00:00Z","state":{"compliance_openscap":"disabled","insights":"disabled","remediations":"disabled"},"org_id":"78606"},{"account":"10064","id":"3c8859ae-ef4e-4136-ab17-ccd4ea9f36bf","created_at":"1970-01-01T00:00:00Z","state":{"compliance_openscap":"enabled","insights":"enabled","remediations":"enabled"},"org_id":"78606"}]}`),
},
},
}
Expand Down Expand Up @@ -256,7 +256,7 @@ func TestGetCurrentState(t *testing.T) {
},
want: response{
code: http.StatusOK,
body: []byte(`{"account":"1","state":{"compliance_openscap":"enabled","insights":"enabled","remediations":"enabled"},"id":"3c8859ae-ef4e-4136-ab17-ccd4ea9f36bf","label":"","apply_state":false,"org_id":"78606"}`),
body: []byte(`{"account":"1","state":{"compliance_openscap":"enabled","insights":"enabled","remediations":"enabled"},"id":"3c8859ae-ef4e-4136-ab17-ccd4ea9f36bf","apply_state":false,"org_id":"78606"}`),
},
},
}
Expand Down Expand Up @@ -319,7 +319,7 @@ func TestGetStateByID(t *testing.T) {
},
want: response{
code: http.StatusOK,
body: []byte(`{"account":"10064","id":"b5db9cbc-4ecd-464b-b416-3a6cd67af87a","label":"","initiator":"","created_at":"1970-01-01T00:00:00Z","state":{"compliance_openscap":"disabled","insights":"disabled","remediations":"disabled"},"org_id":"78606"}`),
body: []byte(`{"account":"10064","id":"b5db9cbc-4ecd-464b-b416-3a6cd67af87a","created_at":"1970-01-01T00:00:00Z","state":{"compliance_openscap":"disabled","insights":"disabled","remediations":"disabled"},"org_id":"78606"}`),
},
},
}
Expand Down
28 changes: 10 additions & 18 deletions internal/http/v2/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -245,33 +245,25 @@
"Profile": {
"type": "object",
"properties": {
"account_id": {
"type": "string",
"description": "Red Hat account number"
},
"active": {
"type": "boolean",
"description": "Remote host configuration enabled state"
},
"created_at": {
"type": "string",
"description": "Time of profile creation"
},
"creator": {
"id": {
"type": "string",
"description": "Username of the profile creator"
"description": "Profile unique identity value"
},
"name": {
"account_id": {
"type": "string",
"description": "Descriptive name for the profile"
"description": "Red Hat account number"
},
"org_id": {
"type": "string",
"description": "Red Hat organization identity value"
},
"id": {
"created_at": {
"type": "string",
"description": "Profile unique identity value"
"description": "Time of profile creation"
},
"active": {
"type": "boolean",
"description": "Remote host configuration enabled state"
},
"compliance": {
"type": "boolean",
Expand Down
39 changes: 19 additions & 20 deletions internal/http/v2/v2.gen.go

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

0 comments on commit 757d0be

Please sign in to comment.