forked from argoproj/argo-cd
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d5dd4df
commit 6f15b3b
Showing
7 changed files
with
190 additions
and
31 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
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,148 @@ | ||
package application | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
appclient "github.com/argoproj/argo-cd/v2/pkg/apiclient/application" | ||
"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" | ||
repoapiclient "github.com/argoproj/argo-cd/v2/reposerver/apiclient" | ||
"google.golang.org/grpc" | ||
"io" | ||
"net/http" | ||
"strings" | ||
"time" | ||
) | ||
|
||
type ApplicationClient interface { | ||
Get(ctx context.Context, in *appclient.ApplicationQuery, opts ...grpc.CallOption) (*v1alpha1.Application, error) | ||
|
||
RevisionMetadata(ctx context.Context, in *appclient.RevisionMetadataQuery, opts ...grpc.CallOption) (*v1alpha1.RevisionMetadata, error) | ||
|
||
GetManifests(ctx context.Context, in *appclient.ApplicationManifestQuery, opts ...grpc.CallOption) (*repoapiclient.ManifestResponse, error) | ||
|
||
ResourceTree(ctx context.Context, in *appclient.ResourcesQuery, opts ...grpc.CallOption) (*v1alpha1.ApplicationTree, error) | ||
|
||
GetResource(ctx context.Context, in *appclient.ApplicationResourceRequest, opts ...grpc.CallOption) (*appclient.ApplicationResourceResponse, error) | ||
|
||
List(ctx context.Context, in *appclient.ApplicationQuery, opts ...grpc.CallOption) (*v1alpha1.ApplicationList, error) | ||
} | ||
|
||
type httpApplicationClient struct { | ||
httpClient *http.Client | ||
baseUrl string | ||
token string | ||
} | ||
|
||
func NewHttpApplicationClient(token string, address string) ApplicationClient { | ||
if !strings.Contains(address, "http") { | ||
address = "http://" + address | ||
} | ||
|
||
return &httpApplicationClient{ | ||
httpClient: &http.Client{ | ||
Timeout: 30 * time.Second, | ||
}, | ||
baseUrl: address, | ||
token: token, | ||
} | ||
} | ||
|
||
func (c *httpApplicationClient) execute(ctx context.Context, url string, result interface{}, printBody ...bool) error { | ||
req, err := http.NewRequestWithContext(ctx, "GET", url, nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
req.Header.Set("Content-Type", "application/json") | ||
req.Header.Set("Authorization", "Bearer "+c.token) | ||
|
||
res, err := c.httpClient.Do(req) | ||
if err != nil { | ||
return err | ||
} | ||
defer res.Body.Close() | ||
|
||
b, _ := io.ReadAll(res.Body) | ||
|
||
isStatusOK := res.StatusCode >= 200 && res.StatusCode < 300 | ||
if !isStatusOK { | ||
return errors.New(fmt.Sprintf("argocd server respond with code %d, msg is: %s", res.StatusCode, string(b))) | ||
} | ||
|
||
err = json.Unmarshal(b, &result) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (c *httpApplicationClient) Get(ctx context.Context, in *appclient.ApplicationQuery, opts ...grpc.CallOption) (*v1alpha1.Application, error) { | ||
url := fmt.Sprintf("%s/api/v1/applications/%s", c.baseUrl, *in.Name) | ||
application := &v1alpha1.Application{} | ||
err := c.execute(ctx, url, application) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return application, nil | ||
} | ||
|
||
func (c *httpApplicationClient) RevisionMetadata(ctx context.Context, in *appclient.RevisionMetadataQuery, opts ...grpc.CallOption) (*v1alpha1.RevisionMetadata, error) { | ||
url := fmt.Sprintf("%s/api/v1/applications/%s/revisions/%s/metadata", c.baseUrl, *in.Name, *in.Revision) | ||
revisionMetadata := &v1alpha1.RevisionMetadata{} | ||
err := c.execute(ctx, url, revisionMetadata) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return revisionMetadata, nil | ||
} | ||
|
||
func (c *httpApplicationClient) GetManifests(ctx context.Context, in *appclient.ApplicationManifestQuery, opts ...grpc.CallOption) (*repoapiclient.ManifestResponse, error) { | ||
url := fmt.Sprintf("%s/api/v1/applications/%s/manifests", c.baseUrl, *in.Name) | ||
|
||
manifest := &repoapiclient.ManifestResponse{} | ||
err := c.execute(ctx, url, manifest) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return manifest, nil | ||
} | ||
|
||
func (c *httpApplicationClient) ResourceTree(ctx context.Context, in *appclient.ResourcesQuery, opts ...grpc.CallOption) (*v1alpha1.ApplicationTree, error) { | ||
url := fmt.Sprintf("%s/api/v1/applications/%s/resource-tree", c.baseUrl, *in.ApplicationName) | ||
tree := &v1alpha1.ApplicationTree{} | ||
err := c.execute(ctx, url, tree) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return tree, nil | ||
} | ||
|
||
func (c *httpApplicationClient) GetResource(ctx context.Context, in *appclient.ApplicationResourceRequest, opts ...grpc.CallOption) (*appclient.ApplicationResourceResponse, error) { | ||
params := fmt.Sprintf("?namespace=%s&resourceName=%s&version=%s&group=%s&kind=%s", | ||
*in.Namespace, | ||
*in.ResourceName, | ||
*in.Version, | ||
*in.Group, | ||
*in.Kind) | ||
url := fmt.Sprintf("%s/api/v1/applications/%s/resource%s", c.baseUrl, *in.Name, params) | ||
|
||
applicationResource := &appclient.ApplicationResourceResponse{} | ||
err := c.execute(ctx, url, applicationResource, true) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return applicationResource, nil | ||
} | ||
|
||
func (c *httpApplicationClient) List(ctx context.Context, in *appclient.ApplicationQuery, opts ...grpc.CallOption) (*v1alpha1.ApplicationList, error) { | ||
url := fmt.Sprintf("%s/api/v1/applications", c.baseUrl) | ||
|
||
apps := &v1alpha1.ApplicationList{} | ||
err := c.execute(ctx, url, apps) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return apps, 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