Skip to content

Commit

Permalink
feat: implement interfaces & crud operations for packages
Browse files Browse the repository at this point in the history
  • Loading branch information
nitishfy committed Oct 18, 2023
1 parent 5d0f837 commit f61305a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
35 changes: 28 additions & 7 deletions obs/packages.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package obs

import (
Expand Down Expand Up @@ -42,7 +58,7 @@ func (o *OBS) CreateUpdatePackage(ctx context.Context, projectName string, pkg *
return fmt.Errorf("creating obs package: joining url: %w", err)
}

resp, err := o.client.InvokeOBSEndpoint(ctx, http.MethodPut, urlPath, bytes.NewBuffer(xmlData))
resp, err := o.client.InvokeOBSEndpoint(ctx, o.options.Username, o.options.Password, http.MethodPut, urlPath, bytes.NewBuffer(xmlData))
if err != nil {
return fmt.Errorf("creating obs package: %w", err)
}
Expand All @@ -69,13 +85,13 @@ func (o *OBS) CreateUpdatePackage(ctx context.Context, projectName string, pkg *
}

// ListPackages lists packages in a given OBS project
func (o *OBS) ListPackages(ctx context.Context, projectName string) (*Package, error) {
func (o *OBS) ListPackages(ctx context.Context, projectName string) ([]string, error) {
urlPath, err := url.JoinPath(o.options.APIURL, "source", projectName)
if err != nil {
return nil, fmt.Errorf("listing obs packages: joining url: %w", err)
}

resp, err := o.client.InvokeOBSEndpoint(ctx, http.MethodGet, urlPath, nil)
resp, err := o.client.InvokeOBSEndpoint(ctx, o.options.Username, o.options.Password, http.MethodGet, urlPath, nil)
if err != nil {
return nil, fmt.Errorf("listing obs packages: invoking obs endpoint: %w", err)
}
Expand Down Expand Up @@ -103,7 +119,12 @@ func (o *OBS) ListPackages(ctx context.Context, projectName string) (*Package, e
return nil, fmt.Errorf("listing obs packages: decoding packages: %w", err)
}

return pkg, nil
packageNames := make([]string, 0)
for _, entry := range pkg.Entries {
packageNames = append(packageNames, entry.Name)
}

return packageNames, nil
}

// GetPackageMetaFile returns package's meta for a given OBS project
Expand All @@ -113,9 +134,9 @@ func (o *OBS) GetPackageMetaFile(ctx context.Context, projectName, packageName s
return nil, fmt.Errorf("getting obs package: joining url: %w", err)
}

resp, err := o.InvokeOBSEndpoint(ctx, http.MethodGet, urlPath, nil)
resp, err := o.client.InvokeOBSEndpoint(ctx, o.options.Username, o.options.Password, http.MethodGet, urlPath, nil)
if err != nil {
return nil, fmt.Errorf("getting obs project: %w", err)
return nil, fmt.Errorf("getting obs package: %w", err)
}
defer resp.Body.Close()

Expand Down Expand Up @@ -151,7 +172,7 @@ func (o *OBS) DeletePackage(ctx context.Context, projectName, packageName string
return fmt.Errorf("deleting obs package: joining url: %w", err)
}

resp, err := o.client.InvokeOBSEndpoint(ctx, http.MethodDelete, urlPath, nil)
resp, err := o.client.InvokeOBSEndpoint(ctx, o.options.Username, o.options.Password, http.MethodDelete, urlPath, nil)
if err != nil {
return fmt.Errorf("deleting obs package: invoking obs endpoint: %w", err)
}
Expand Down
17 changes: 8 additions & 9 deletions obs/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,15 @@ type OBS struct {

type obsClient struct {
*http.Client
*Options
}

// Client is an interface modeling supported OBS operations
type Client interface {
InvokeOBSEndpoint(ctx context.Context, method string, url string, xml *bytes.Buffer) (*http.Response, error)
InvokeOBSEndpoint(ctx context.Context, username string, password string, method string, URL string, xml *bytes.Buffer) (*http.Response, error)
}

// InvokeOBSEndpoint invokes an OBS endpoint by making a HTTP request
func (o *OBS) InvokeOBSEndpoint(ctx context.Context, method string, url string, xmlData *bytes.Buffer) (*http.Response, error) {
func (o *obsClient) InvokeOBSEndpoint(ctx context.Context, username string, password string, method string, url string, xmlData *bytes.Buffer) (*http.Response, error) {
req, err := http.NewRequestWithContext(ctx, method, url, xmlData)
if err != nil {
return nil, &APIError{
Expand All @@ -67,10 +66,10 @@ func (o *OBS) InvokeOBSEndpoint(ctx context.Context, method string, url string,
}
}

req.SetBasicAuth(o.options.Username, o.options.Password)
req.SetBasicAuth(username, password)
req.Header.Set("Accept", "application/xml; charset=utf-8")

resp, err := o.InvokeOBSEndpoint(ctx, method, url, xmlData)
resp, err := o.Client.Do(req)
if err != nil {
return nil, &APIError{
HTTPStatusCode: 0,
Expand All @@ -92,7 +91,7 @@ type Options struct {
// DefaultOptions return an options struct with commonly used settings
func DefaultOptions() *Options {
return &Options{
APIURL: "https://build.opensuse.org/",
APIURL: "https://api.opensuse.org/",
}
}

Expand Down Expand Up @@ -167,7 +166,7 @@ func (o *OBS) CreateUpdateProject(ctx context.Context, project *Project) error {
return fmt.Errorf("creating obs project: joining url: %w", err)
}

resp, err := o.client.InvokeOBSEndpoint(ctx, http.MethodPut, urlPath, bytes.NewBuffer(xmlData))
resp, err := o.client.InvokeOBSEndpoint(ctx, o.options.Username, o.options.Password, http.MethodPut, urlPath, bytes.NewBuffer(xmlData))
if err != nil {
return fmt.Errorf("creating obs project: %w", err)
}
Expand Down Expand Up @@ -200,7 +199,7 @@ func (o *OBS) GetProjectMetaFile(ctx context.Context, projectName string) (*Proj
return nil, fmt.Errorf("getting obs project: joining url: %w", err)
}

resp, err := o.InvokeOBSEndpoint(ctx, http.MethodGet, urlPath, nil)
resp, err := o.client.InvokeOBSEndpoint(ctx, o.options.Username, o.options.Password, http.MethodGet, urlPath, nil)
if err != nil {
return nil, fmt.Errorf("getting obs project: %w", err)
}
Expand Down Expand Up @@ -238,7 +237,7 @@ func (o *OBS) DeleteProject(ctx context.Context, project *Project) error {
return fmt.Errorf("deleting obs project: joining url: %w", err)
}

resp, err := o.client.InvokeOBSEndpoint(ctx, http.MethodDelete, urlPath, nil)
resp, err := o.client.InvokeOBSEndpoint(ctx, o.options.Username, o.options.Password, http.MethodDelete, urlPath, nil)
if err != nil {
return fmt.Errorf("deleting obs project: %w", err)
}
Expand Down

0 comments on commit f61305a

Please sign in to comment.