summaryrefslogtreecommitdiff
path: root/internal/discovery/discovery.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/discovery/discovery.go')
-rw-r--r--internal/discovery/discovery.go96
1 files changed, 48 insertions, 48 deletions
diff --git a/internal/discovery/discovery.go b/internal/discovery/discovery.go
index 81163f8..80a69eb 100644
--- a/internal/discovery/discovery.go
+++ b/internal/discovery/discovery.go
@@ -293,44 +293,20 @@ func (discovery *Discovery) DetermineServersUpdate() bool {
return !time.Now().Before(upd)
}
-func (discovery *Discovery) previousOrganizations() (*Organizations, error) {
- // If the version field is not zero then we have a cached struct
- // We also immediately return this copy if we have no embedded JSON
- if discovery.OrganizationList.Version != 0 || !HasCache {
- return &discovery.OrganizationList, nil
+func (discovery *Discovery) cachedOrgs() *Organizations {
+ if discovery.OrganizationList.Version == 0 {
+ return nil
}
-
- // We do not have a cached struct, this we need to get it using the embedded JSON
- var eo Organizations
- if err := json.Unmarshal(eOrganizations, &eo); err != nil {
- return nil, fmt.Errorf("failed parsing discovery organizations from the embedded cache with error: %w", err)
- }
- discovery.OrganizationList = eo
- return &eo, nil
-}
-
-func (discovery *Discovery) previousServers() (*Servers, error) {
- // If the version field is not zero then we have a cached struct
- // We also immediately return this copy if we have no embedded JSON
- if discovery.ServerList.Version != 0 || !HasCache {
- return &discovery.ServerList, nil
- }
-
- // We do not have a cached struct, this we need to get it using the embedded JSON
- var es Servers
- if err := json.Unmarshal(eServers, &es); err != nil {
- return nil, fmt.Errorf("failed parsing discovery servers from the embedded cache with error: %w", err)
- }
- discovery.ServerList = es
- return &es, nil
+ return &discovery.OrganizationList
}
// Organizations returns the discovery organizations
// The second return value is a boolean that indicates whether a fresh list was updated internally
// If there was an error, a cached copy is returned if available.
-func (discovery *Discovery) Organizations(ctx context.Context) (*Organizations, bool, error) {
- if !discovery.DetermineOrganizationsUpdate() {
- return &discovery.OrganizationList, false, nil
+// cache is set to true if there should be no network call done
+func (discovery *Discovery) Organizations(ctx context.Context, cache bool) (*Organizations, bool, error) {
+ if cache || !discovery.DetermineOrganizationsUpdate() {
+ return discovery.cachedOrgs(), false, nil
}
file := "organization_list.json"
var jsonDecode Organizations
@@ -347,11 +323,7 @@ func (discovery *Discovery) Organizations(ctx context.Context) (*Organizations,
}
}
// Return previous with an error
- orgs, perr := discovery.previousOrganizations()
- if perr != nil {
- slog.Warn("failed to get previous discovery organizations", "error", perr)
- }
- return orgs, false, err
+ return discovery.cachedOrgs(), false, err
}
if len(jsonDecode.List) == 0 {
slog.Warn("fresh organization list is empty")
@@ -365,12 +337,20 @@ func (discovery *Discovery) Organizations(ctx context.Context) (*Organizations,
return &discovery.OrganizationList, true, nil
}
+func (discovery *Discovery) cachedServers() *Servers {
+ if discovery.ServerList.Version == 0 {
+ return nil
+ }
+ return &discovery.ServerList
+}
+
// Servers returns the discovery servers
// The second return value is a boolean that indicates whether a fresh list was updated internally
// If there was an error, a cached copy is returned if available.
-func (discovery *Discovery) Servers(ctx context.Context) (*Servers, bool, error) {
- if !discovery.DetermineServersUpdate() {
- return &discovery.ServerList, false, nil
+// cache is set to true if there should be no network call done
+func (discovery *Discovery) Servers(ctx context.Context, cache bool) (*Servers, bool, error) {
+ if cache || !discovery.DetermineServersUpdate() {
+ return discovery.cachedServers(), false, nil
}
file := "server_list.json"
var jsonDecode Servers
@@ -387,11 +367,7 @@ func (discovery *Discovery) Servers(ctx context.Context) (*Servers, bool, error)
}
}
// Return previous with an error
- srvs, perr := discovery.previousServers()
- if perr != nil {
- slog.Warn("failed to get previous discovery server", "error", perr)
- }
- return srvs, false, err
+ return discovery.cachedServers(), false, err
}
if len(jsonDecode.List) == 0 {
slog.Warn("fresh server list is empty")
@@ -407,9 +383,17 @@ func (discovery *Discovery) Servers(ctx context.Context) (*Servers, bool, error)
// UpdateServers updates the discovery servers to the new version
// It does this by checking versions
-func (discovery *Discovery) UpdateServers(other Discovery) {
- if other.ServerList.Version >= discovery.ServerList.Version {
- discovery.ServerList = other.ServerList
+func (discovery *Discovery) UpdateServers(other Servers) {
+ if other.Version >= discovery.ServerList.Version {
+ discovery.ServerList = other
+ }
+}
+
+// UpdateOrganizations updates the discovery organizations to the new version
+// It does this by checking versions
+func (discovery *Discovery) UpdateOrganizations(other Organizations) {
+ if other.Version >= discovery.OrganizationList.Version {
+ discovery.OrganizationList = other
}
}
@@ -429,3 +413,19 @@ func (discovery *Discovery) Copy() (Discovery, error) {
return dest, nil
}
+
+// Fill makes sure that the cache is filled with the embedded discovery
+func (discovery *Discovery) Fill() error {
+ if !HasCache {
+ return nil
+ }
+
+ var err error
+ var es Servers
+ err = errors.Join(err, json.Unmarshal(eServers, &es))
+ discovery.UpdateServers(es)
+ var eo Organizations
+ err = errors.Join(err, json.Unmarshal(eOrganizations, &eo))
+ discovery.UpdateOrganizations(eo)
+ return err
+}