Skip to content

Commit

Permalink
Merge pull request #15 from jamestoyer/get-replications-fix
Browse files Browse the repository at this point in the history
Get replication configuration fixed for repos with only one replication
  • Loading branch information
Dillon Giacoppo authored Jun 25, 2019
2 parents 9b64e51 + 3271e10 commit 8c0aa03
Showing 1 changed file with 56 additions and 11 deletions.
67 changes: 56 additions & 11 deletions artifactory/v1/artifacts.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package v1

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
)

Expand Down Expand Up @@ -59,7 +61,7 @@ func (s *ArtifactService) SetRepositoryReplicationConfig(ctx context.Context, re
func (s *ArtifactService) SetSingleRepositoryReplicationConfig(ctx context.Context, repoKey string, config *SingleReplicationConfig) (*http.Response, error) {
path := fmt.Sprintf("/api/replications/%s", repoKey)
req, err := s.client.NewJSONEncodedRequest("PUT", path, config)
if err !=nil {
if err != nil {
return nil, err
}

Expand All @@ -70,16 +72,7 @@ func (s *ArtifactService) SetSingleRepositoryReplicationConfig(ctx context.Conte
// Notes: Requires Artifactory Pro
// Security: Requires a privileged user
func (s *ArtifactService) GetRepositoryReplicationConfig(ctx context.Context, repoKey string) (*ReplicationConfig, *http.Response, error) {
path := fmt.Sprintf("/api/replications/%s", repoKey)
req, err := s.client.NewRequest("GET", path, nil)
if err != nil {
return nil, nil, err
}
req.Header.Set("Accept", mediaTypeReplicationConfig)

replications := make([]SingleReplicationConfig, 0)
resp, err := s.client.Do(ctx, req, &replications)

replications, resp, err := s.getReplicationConfigs(ctx, repoKey)
if err != nil {
return nil, resp, err
}
Expand All @@ -101,6 +94,58 @@ func (s *ArtifactService) GetRepositoryReplicationConfig(ctx context.Context, re
return replicationConfig, resp, nil
}

// Gets the replication configs for a given repository key.
// Note: As the get endpoint can return a single object or an array (if there is more than one replication), extra work
// is required to marshall the response into an expected, consistent format.
func (s *ArtifactService) getReplicationConfigs(ctx context.Context, repoKey string) ([]SingleReplicationConfig, *http.Response, error) {
path := fmt.Sprintf("/api/replications/%s", repoKey)
req, err := s.client.NewRequest("GET", path, nil)
if err != nil {
return nil, nil, err
}
req.Header.Set("Accept", mediaTypeReplicationConfig)

// By writing the response to a buffer, we can evaluate the type and decode appropriately.
var httpBody bytes.Buffer
resp, err := s.client.Do(ctx, req, &httpBody)
if err != nil {
return nil, resp, err
}

// A copy is required as the initial write to the httpBody buffer contains EOF issues.
var httpBodyCopy bytes.Buffer
_, err = io.Copy(&httpBodyCopy, &httpBody)
if err != nil {
return nil, resp, err
}

var repConfigAsInterface interface{}
err = json.Unmarshal(httpBodyCopy.Bytes(), &repConfigAsInterface)
if err != nil {
return nil, resp, err
}

// Checks to see what type of response is returned, and then casts to that.
replications := make([]SingleReplicationConfig, 0)
switch repConfigAsInterface.(type) {
case []interface{}:
err = json.NewDecoder(&httpBodyCopy).Decode(&replications)
if err != nil {
return nil, resp, err
}
default:
singleReplication := new(SingleReplicationConfig)
err = json.NewDecoder(&httpBodyCopy).Decode(singleReplication)
if err != nil {
return nil, resp, err
}

replications = append(replications, *singleReplication)
}

return replications, resp, nil
}

// Updates a local multi-push replication configuration. Supported by local repositories.
// Notes: Requires an enterprise license
// Security: Requires a privileged user
Expand Down

0 comments on commit 8c0aa03

Please sign in to comment.