generated from CDCgov/template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #251 from CDCgov/config-retrieval
Config retrieval
- Loading branch information
Showing
19 changed files
with
177 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"isActive": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"isActive": true | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package config | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"github.com/CDCgov/reportstream-sftp-ingestion/utils" | ||
"log/slog" | ||
"slices" | ||
) | ||
|
||
type PartnerSettings struct { | ||
DisplayName string `json:"displayName"` // full name if we need pretty names | ||
IsActive bool `json:"isActive"` | ||
IsExternalSftpConnection bool `json:"isExternalSftpConnection"` | ||
HasZipPassword bool `json:"hasZipPassword"` | ||
DefaultEncoding string `json:"defaultEncoding"` | ||
} | ||
|
||
/* | ||
TODO list as of Dec 10: | ||
Current PR: | ||
- Add tests for NewConfig? | ||
- Set up config files for ca-phl and flexion in all envs (need to change what's in local too - it currently only has one value) | ||
- Do we want to add some kind of logging indicating what config got loaded? Maybe just temporarily for testing purposes? | ||
- ADR for not-crashing if one config fails | ||
- ADR for config in general | ||
Future PR: | ||
- Set up another function trigger/CRON for Flexion | ||
- What happens if you try to retrieve a map index that doesn't exist? Need to check for errors or nils or something everywhere we get config | ||
- In polling message handler, use queue message to: | ||
- decide whether to do retrieval ('no' for flexion probs) | ||
- build key names for retrieving secrets | ||
- build file paths for saving files (both zips and hl7s) | ||
- add config to tests | ||
- In import message handler: | ||
- parse file path to get partner ID | ||
- use partner ID to build key names for retrieving secrets to call RS | ||
- add config to tests | ||
- See if we need to do add'l TF to set up Flexion? | ||
- probably at least a cron expression and RS config. It would be nice to have an external Flexion SFTP site to hit for testing | ||
- Do we want to start making TF dynamic at this point or wait for add'l partners? I think maybe wait for 1-2 more partners? | ||
*/ | ||
|
||
func populatePartnerSettings(input []byte, partnerId string) (PartnerSettings, error) { | ||
|
||
var partnerSettings PartnerSettings | ||
err := json.Unmarshal(input, &partnerSettings) | ||
|
||
if err != nil { | ||
slog.Error("Unable unmarshall to partner settings", slog.Any(utils.ErrorKey, err)) | ||
return PartnerSettings{}, err | ||
} | ||
|
||
err = validateDefaultEncoding(partnerSettings.DefaultEncoding) | ||
if err != nil { | ||
slog.Error("Invalid encoding found", slog.Any(utils.ErrorKey, err), slog.String("Partner ID", partnerId), slog.String("Encoding", partnerSettings.DefaultEncoding)) | ||
return PartnerSettings{}, err | ||
} | ||
|
||
// TODO - any other validation? | ||
|
||
return partnerSettings, nil | ||
} | ||
|
||
func validateDefaultEncoding(input string) error { | ||
if slices.Contains(allowedEncodingList, input) { | ||
return nil | ||
} | ||
return errors.New("Invalid encoding found: " + input) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package config | ||
|
||
import ( | ||
"github.com/CDCgov/reportstream-sftp-ingestion/storage" | ||
"github.com/CDCgov/reportstream-sftp-ingestion/utils" | ||
"log/slog" | ||
"time" | ||
) | ||
|
||
type Config struct { | ||
// PartnerId is a unique name to identify a partner. It's put in queue message from polling function and used in blob paths | ||
PartnerId string | ||
lastRetrieved time.Time | ||
partnerSettings PartnerSettings | ||
} | ||
|
||
// TODO confirm if these should stay here in config or move to constants | ||
var allowedEncodingList = []string{"ISO-8859-1", "UTF-8"} | ||
var KnownPartnerIds = []string{utils.CA_PHL, utils.FLEXION} | ||
var Configs = make(map[string]*Config) | ||
|
||
func init() { | ||
for _, partnerId := range KnownPartnerIds { | ||
partnerConfig, err := NewConfig(partnerId) | ||
if err != nil { | ||
// TODO - add an ADR talking about this. We're not crashing if a single config doesn't load in case only one partner is impacted | ||
slog.Error("Unable to load or parse config", slog.Any(utils.ErrorKey, err), slog.String("partner", partnerId)) | ||
} | ||
|
||
Configs[partnerId] = partnerConfig | ||
slog.Info("config found", slog.String("Id", partnerId)) | ||
|
||
} | ||
} | ||
|
||
func NewConfig(partnerId string) (*Config, error) { | ||
// Create blob client | ||
handler, err := storage.NewAzureBlobHandler() | ||
if err != nil { | ||
slog.Error("Failed to create Azure Blob handler for config retrieval", slog.Any(utils.ErrorKey, err), slog.String("partnerId", partnerId)) | ||
return nil, err | ||
} | ||
|
||
// Retrieve settings file from Azure | ||
fileContents, err := handler.FetchFile("config", partnerId+".json") | ||
if err != nil { | ||
slog.Error("Failed to retrieve partner settings", slog.Any(utils.ErrorKey, err), slog.String("partnerId", partnerId)) | ||
return nil, err | ||
} | ||
|
||
// Parse file content by calling populate | ||
partnerSettings, err := populatePartnerSettings(fileContents, partnerId) | ||
if err != nil { | ||
// We log any errors in the called function | ||
return nil, err | ||
} | ||
|
||
// Set up config object | ||
config := &Config{} | ||
config.lastRetrieved = time.Now().UTC() | ||
config.PartnerId = partnerId | ||
config.partnerSettings = partnerSettings | ||
|
||
return config, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.