Skip to content

Commit

Permalink
Handle error messages from OCM
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Shen <[email protected]>
  • Loading branch information
mjlshen authored and tonytheleg committed Jun 5, 2024
1 parent 5294659 commit a8e9a42
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 26 deletions.
2 changes: 1 addition & 1 deletion frontend/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ frontend:
go build -o aro-hcp-frontend .

run:
./aro-hcp-frontend --use-cache --region eastus --clusters-service-url http://localhost:8000
./aro-hcp-frontend --use-cache --region ${REGION} --clusters-service-url http://localhost:8000

clean:
rm -f aro-hcp-frontend
Expand Down
14 changes: 7 additions & 7 deletions frontend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ make undeploy-private

Update a subscription state (Must be **Registered** for other calls to function)
```bash
curl -X PUT localhost:8443/subscriptions/YOUR_SUBSCRIPTION_ID?api-version=2.0 --json '{"state":"Registered"}'
curl -X PUT localhost:8443/subscriptions/00000000-0000-0000-0000-000000000000?api-version=2.0 --json '{"state":"Registered"}'
```

List the Operations for the Provider
Expand All @@ -68,30 +68,30 @@ curl -X GET "localhost:8443/providers/Microsoft.RedHatOpenshift/operations?api-v
List HcpOpenShiftVersions Resources by Location

```bash
curl -X GET "localhost:8443/subscriptions/YOUR_SUBSCRIPTION_ID/locations/YOUR_LOCATION/providers/Microsoft.RedHatOpenshift/hcpOpenShiftVersions?api-version=2024-06-10-preview"
curl -X GET "localhost:8443/subscriptions/00000000-0000-0000-0000-000000000000/locations/YOUR_LOCATION/providers/Microsoft.RedHatOpenshift/hcpOpenShiftVersions?api-version=2024-06-10-preview"
```

List HcpOpenShiftClusterResource Resources by Subscription ID
```bash
curl -X GET "localhost:8443/subscriptions/YOUR_SUBSCRIPTION_ID/providers/Microsoft.RedHatOpenshift/hcpOpenShiftClusters?api-version=2024-06-10-preview"
curl -X GET "localhost:8443/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.RedHatOpenshift/hcpOpenShiftClusters?api-version=2024-06-10-preview"
```

Get a HcpOpenShiftClusterResource
```bash
curl -X GET "localhost:8443/subscriptions/YOUR_SUBSCRIPTION_ID/resourceGroups/YOUR_RESOURCE_GROUP_NAME/providers/Microsoft.RedHatOpenshift/hcpOpenShiftClusters/YOUR_CLUSTER_NAME?api-version=2024-06-10-preview"
curl -X GET "localhost:8443/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/dev-test-rg/providers/Microsoft.RedHatOpenshift/hcpOpenShiftClusters/YOUR_CLUSTER_NAME?api-version=2024-06-10-preview"
```

Create or Update a HcpOpenShiftClusterResource
```bash
curl -X PUT "localhost:8443/subscriptions/YOUR_SUBSCRIPTION_ID/resourceGroups/YOUR_RESOURCE_GROUP_NAME/providers/Microsoft.RedHatOpenshift/hcpOpenShiftClusters/YOUR_CLUSTER_NAME?api-version=2024-06-10-preview" --json @cluster.json
curl -X PUT "localhost:8443/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/dev-test-rg/providers/Microsoft.RedHatOpenshift/hcpOpenShiftClusters/YOUR_CLUSTER_NAME?api-version=2024-06-10-preview" --json @cluster.json
```

Delete a HcpOpenShiftClusterResource
```bash
curl -X DELETE "localhost:8443/subscriptions/YOUR_SUBSCRIPTION_ID/resourceGroups/YOUR_RESOURCE_GROUP_NAME/providers/Microsoft.RedHatOpenshift/hcpOpenShiftClusters/YOUR_CLUSTER_NAME?api-version=2024-06-10-preview"
curl -X DELETE "localhost:8443/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/dev-test-rg/providers/Microsoft.RedHatOpenshift/hcpOpenShiftClusters/YOUR_CLUSTER_NAME?api-version=2024-06-10-preview"
```

Execute deployment preflight checks
```bash
curl -X POST "localhost:8443/subscriptions/YOUR_SUBSCRIPTION_ID/resourceGroups/YOUR_RESOURCE_GROUP_NAME/providers/Microsoft.RedHatOpenshift/deployments/YOUR_DEPLOYMENT_NAME/preflight?api-version=2020-06-01" --json preflight.json
curl -X POST "localhost:8443/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/dev-test-rg/providers/Microsoft.RedHatOpenshift/deployments/YOUR_DEPLOYMENT_NAME/preflight?api-version=2020-06-01" --json preflight.json
```
2 changes: 1 addition & 1 deletion frontend/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ toolchain go1.22.2

require (
github.com/Azure/ARO-HCP/internal v0.0.0-00010101000000-000000000000
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.11.1
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.5.2
github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos v1.0.1
github.com/google/go-cmp v0.6.0
Expand All @@ -19,7 +20,6 @@ require (

require (
github.com/Azure/azure-sdk-for-go v68.0.0+incompatible // indirect
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.11.1
github.com/Azure/azure-sdk-for-go/sdk/internal v1.7.0 // indirect
github.com/AzureAD/microsoft-authentication-library-for-go v1.2.2 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
Expand Down
24 changes: 21 additions & 3 deletions frontend/pkg/frontend/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,20 @@ func (f *Frontend) ArmResourceRead(writer http.ResponseWriter, request *http.Req
}
}

cluster, _ := f.conn.ClustersMgmt().V1().Clusters().Cluster(doc.ClusterID).Get().Send()
hcpCluster, _ := ocm.ConvertCStoFrontend(*cluster.Body())
cluster, err := f.conn.ClustersMgmt().V1().Clusters().Cluster(doc.ClusterID).Get().Send()
if err != nil {
f.logger.Error(fmt.Sprintf("cluster not found in clusters-service: %v", err))
writer.WriteHeader(http.StatusNoContent)
return
}

hcpCluster, err := ocm.ConvertCStoHCPOpenShiftCluster(cluster.Body())
if err != nil {
// Should never happen currently
f.logger.Error(err.Error())
arm.WriteInternalServerError(writer)
return
}

versionedResource := versionedInterface.NewHCPOpenShiftCluster(hcpCluster)
resp, err := json.Marshal(versionedResource)
Expand Down Expand Up @@ -343,7 +355,13 @@ func (f *Frontend) ArmResourceCreateOrUpdate(writer http.ResponseWriter, request
return
}
if csResp.Body() != nil {
hcpCluster, _ = ocm.ConvertCStoFrontend(*csResp.Body())
hcpCluster, err = ocm.ConvertCStoHCPOpenShiftCluster(csResp.Body())
if err != nil {
// Should never happen currently
f.logger.Error(err.Error())
arm.WriteInternalServerError(writer)
return
}
}
}
versionedCurrentCluster := versionedInterface.NewHCPOpenShiftCluster(hcpCluster)
Expand Down
20 changes: 11 additions & 9 deletions frontend/pkg/frontend/middleware_validatestatic.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,19 @@ func MiddlewareValidateStatic(w http.ResponseWriter, r *http.Request, next http.
}
}

if resourceID.Name != "" && resourceID.ResourceType.String() != "Microsoft.Resources/subscriptions" {
if !rxResourceName.MatchString(resourceID.Name) {
arm.WriteError(w, http.StatusBadRequest,
arm.CloudErrorInvalidResourceName,
resourceID.String(),
"The Resource '%s/%s' under resource group '%s' is invalid.",
resourceID.ResourceType, resourceID.Name,
resourceID.ResourceGroupName)
return
if resourceID.Name != "" {
if !rxResourceName.MatchString(resourceID.Name) {
arm.WriteError(w, http.StatusBadRequest,
arm.CloudErrorInvalidResourceName,
resourceID.String(),
"The Resource '%s/%s' under resource group '%s' is invalid.",
resourceID.ResourceType, resourceID.Name,
resourceID.ResourceGroupName)
return
}
}
}
}

next(w, r)
}
16 changes: 11 additions & 5 deletions frontend/pkg/ocm/ocm.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ const (
csCCSEnabled bool = true
)

// ConvertCStoFrontend converts a CS Cluster object into HCPOpenShiftCluster object
func ConvertCStoFrontend(cluster cmv1.Cluster) (*api.HCPOpenShiftCluster, error) {
hcpcluster := api.HCPOpenShiftCluster{
// ConvertCStoHCPOpenShiftCluster converts a CS Cluster object into HCPOpenShiftCluster object
func ConvertCStoHCPOpenShiftCluster(cluster *cmv1.Cluster) (*api.HCPOpenShiftCluster, error) {
hcpcluster := &api.HCPOpenShiftCluster{
TrackedResource: arm.TrackedResource{
Location: "",
Tags: nil,
Expand Down Expand Up @@ -83,12 +83,18 @@ func ConvertCStoFrontend(cluster cmv1.Cluster) (*api.HCPOpenShiftCluster, error)
Enabled: false,
ExternalAuths: []*v1.OIDCProvider{},
},
Ingress: []*api.IngressProfile{},
Ingress: []*api.IngressProfile{
{
IP: "",
URL: "",
Visibility: "",
},
},
},
},
}

return &hcpcluster, nil
return hcpcluster, nil
}

// BuildCSCluster creates a CS Cluster object from an HCPOpenShiftCluster object
Expand Down

0 comments on commit a8e9a42

Please sign in to comment.