Skip to content

Commit

Permalink
Allow cALM service key for cTMS steps (#4661)
Browse files Browse the repository at this point in the history
* Allow cALM service keys

* Fix typo

Co-authored-by: Srinikitha Kondreddy <[email protected]>

* fix typo

Co-authored-by: Srinikitha Kondreddy <[email protected]>

* Hardcode tms endpoint in calm test case

* Add new serviceKey parameter

* Use new serviceKey parameter

With deprecation warning if old tmsServiceKey parameter is used

* Add unit tests and optimise

* Remove tms from service key log message

* Apply suggestions from code review

Co-authored-by: Artem Bannikov <[email protected]>

* Remove unused json fields mapping

* Apply review suggestion

* Apply further review suggestions

* Use new parameter name in groovy

* Generate again

* Fix groovy test

---------

Co-authored-by: Srinikitha Kondreddy <[email protected]>
Co-authored-by: Artem Bannikov <[email protected]>
  • Loading branch information
3 people authored Nov 27, 2023
1 parent c6c02fc commit 17de9ed
Show file tree
Hide file tree
Showing 13 changed files with 263 additions and 40 deletions.
6 changes: 5 additions & 1 deletion cmd/tmsExport.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ func runTmsExport(exportConfig tmsExportOptions, communicationInstance tms.Commu

func convertExportOptions(exportConfig tmsExportOptions) tms.Options {
var config tms.Options
config.TmsServiceKey = exportConfig.TmsServiceKey
config.ServiceKey = exportConfig.ServiceKey
if exportConfig.ServiceKey == "" && exportConfig.TmsServiceKey != "" {
config.ServiceKey = exportConfig.TmsServiceKey
log.Entry().Warn("DEPRECATION WARNING: The tmsServiceKey parameter has been deprecated, please use the serviceKey parameter instead.")
}
config.CustomDescription = exportConfig.CustomDescription
if config.CustomDescription == "" {
config.CustomDescription = tms.DEFAULT_TR_DESCRIPTION
Expand Down
26 changes: 19 additions & 7 deletions cmd/tmsExport_generated.go

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

47 changes: 47 additions & 0 deletions cmd/tmsExport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,50 @@ func TestRunTmsExport(t *testing.T) {
assert.EqualError(t, err, "failed to export file to node: Something went wrong on exporting file to node")
})
}

func Test_convertExportOptions(t *testing.T) {
t.Parallel()
mockServiceKey := `no real serviceKey json necessary for these tests`

t.Run("Use of new serviceKey parameter works", func(t *testing.T) {
t.Parallel()

// init
config := tmsExportOptions{ServiceKey: mockServiceKey}
wantOptions := tms.Options{ServiceKey: mockServiceKey, CustomDescription: "Created by Piper"}

// test
gotOptions := convertExportOptions(config)

// assert
assert.Equal(t, wantOptions, gotOptions)
})

t.Run("Use of old tmsServiceKey parameter works as well", func(t *testing.T) {
t.Parallel()

// init
config := tmsExportOptions{TmsServiceKey: mockServiceKey}
wantOptions := tms.Options{ServiceKey: mockServiceKey, CustomDescription: "Created by Piper"}

// test
gotOptions := convertExportOptions(config)

// assert
assert.Equal(t, wantOptions, gotOptions)
})

t.Run("Use of both tmsServiceKey and serviceKey parameter favors the new serviceKey parameter", func(t *testing.T) {
t.Parallel()

// init
config := tmsExportOptions{ServiceKey: mockServiceKey, TmsServiceKey: "some other string"}
wantOptions := tms.Options{ServiceKey: mockServiceKey, CustomDescription: "Created by Piper"}

// test
gotOptions := convertExportOptions(config)

// assert
assert.Equal(t, wantOptions, gotOptions)
})
}
6 changes: 5 additions & 1 deletion cmd/tmsUpload.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ func runTmsUpload(uploadConfig tmsUploadOptions, communicationInstance tms.Commu

func convertUploadOptions(uploadConfig tmsUploadOptions) tms.Options {
var config tms.Options
config.TmsServiceKey = uploadConfig.TmsServiceKey
config.ServiceKey = uploadConfig.ServiceKey
if uploadConfig.ServiceKey == "" && uploadConfig.TmsServiceKey != "" {
config.ServiceKey = uploadConfig.TmsServiceKey
log.Entry().Warn("DEPRECATION WARNING: The tmsServiceKey parameter has been deprecated, please use the serviceKey parameter instead.")
}
config.CustomDescription = uploadConfig.CustomDescription
if config.CustomDescription == "" {
config.CustomDescription = tms.DEFAULT_TR_DESCRIPTION
Expand Down
26 changes: 19 additions & 7 deletions cmd/tmsUpload_generated.go

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

47 changes: 47 additions & 0 deletions cmd/tmsUpload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,3 +506,50 @@ func TestRunTmsUpload(t *testing.T) {
assert.EqualError(t, err, "failed to upload file to node: Something went wrong on uploading file to node")
})
}

func Test_convertUploadOptions(t *testing.T) {
t.Parallel()
mockServiceKey := `no real serviceKey json necessary for these tests`

t.Run("Use of new serviceKey parameter works", func(t *testing.T) {
t.Parallel()

// init
config := tmsUploadOptions{ServiceKey: mockServiceKey}
wantOptions := tms.Options{ServiceKey: mockServiceKey, CustomDescription: "Created by Piper"}

// test
gotOptions := convertUploadOptions(config)

// assert
assert.Equal(t, wantOptions, gotOptions)
})

t.Run("Use of old tmsServiceKey parameter works as well", func(t *testing.T) {
t.Parallel()

// init
config := tmsUploadOptions{TmsServiceKey: mockServiceKey}
wantOptions := tms.Options{ServiceKey: mockServiceKey, CustomDescription: "Created by Piper"}

// test
gotOptions := convertUploadOptions(config)

// assert
assert.Equal(t, wantOptions, gotOptions)
})

t.Run("Use of both tmsServiceKey and serviceKey parameter favors the new serviceKey parameter", func(t *testing.T) {
t.Parallel()

// init
config := tmsUploadOptions{ServiceKey: mockServiceKey, TmsServiceKey: "some other string"}
wantOptions := tms.Options{ServiceKey: mockServiceKey, CustomDescription: "Created by Piper"}

// test
gotOptions := convertUploadOptions(config)

// assert
assert.Equal(t, wantOptions, gotOptions)
})
}
40 changes: 27 additions & 13 deletions pkg/tms/tmsUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ type uaa struct {
}

type serviceKey struct {
Uaa uaa `json:"uaa"`
Uri string `json:"uri"`
Uaa uaa `json:"uaa"`
Uri string `json:"uri"`
CALMEndpoints cALMEndpoints `json:"endpoints"`
}

type cALMEndpoints *struct {
API string `json:"Api"`
}

type CommunicationInstance struct {
Expand Down Expand Up @@ -105,15 +110,15 @@ type CommunicationInterface interface {
}

type Options struct {
TmsServiceKey string `json:"tmsServiceKey,omitempty"`
CustomDescription string `json:"customDescription,omitempty"`
NamedUser string `json:"namedUser,omitempty"`
NodeName string `json:"nodeName,omitempty"`
MtaPath string `json:"mtaPath,omitempty"`
MtaVersion string `json:"mtaVersion,omitempty"`
NodeExtDescriptorMapping map[string]interface{} `json:"nodeExtDescriptorMapping,omitempty"`
Proxy string `json:"proxy,omitempty"`
StashContent []string `json:"stashContent,omitempty"`
ServiceKey string
CustomDescription string
NamedUser string
NodeName string
MtaPath string
MtaVersion string
NodeExtDescriptorMapping map[string]interface{}
Proxy string
StashContent []string
Verbose bool
}

Expand All @@ -123,6 +128,7 @@ type tmsUtilsBundle struct {
}

const DEFAULT_TR_DESCRIPTION = "Created by Piper"
const CALM_REROUTING_ENDPOINT_TO_CTMS = "/imp-cdm-transport-management-api/v1"

func NewTmsUtils() TmsUtils {
utils := tmsUtilsBundle{
Expand All @@ -140,6 +146,14 @@ func unmarshalServiceKey(serviceKeyJson string) (serviceKey serviceKey, err erro
if err != nil {
return
}
if len(serviceKey.Uri) == 0 {
if serviceKey.CALMEndpoints != nil && len(serviceKey.CALMEndpoints.API) > 0 {
serviceKey.Uri = serviceKey.CALMEndpoints.API + CALM_REROUTING_ENDPOINT_TO_CTMS
} else {
err = fmt.Errorf("neither uri nor endpoints.Api is set in service key json string")
return
}
}
return
}

Expand Down Expand Up @@ -237,9 +251,9 @@ func SetupCommunication(config Options) (communicationInstance CommunicationInte
}
}

serviceKey, err := unmarshalServiceKey(config.TmsServiceKey)
serviceKey, err := unmarshalServiceKey(config.ServiceKey)
if err != nil {
log.Entry().WithError(err).Fatal("Failed to unmarshal TMS service key")
log.Entry().WithError(err).Fatal("Failed to unmarshal service key")
}
log.RegisterSecret(serviceKey.Uaa.ClientSecret)

Expand Down
Loading

0 comments on commit 17de9ed

Please sign in to comment.