|  | 
|  | 1 | +// Copyright Mia srl | 
|  | 2 | +// SPDX-License-Identifier: Apache-2.0 | 
|  | 3 | +// | 
|  | 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | +// you may not use this file except in compliance with the License. | 
|  | 6 | +// You may obtain a copy of the License at | 
|  | 7 | +// | 
|  | 8 | +//     http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | +// | 
|  | 10 | +// Unless required by applicable law or agreed to in writing, software | 
|  | 11 | +// distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | +// See the License for the specific language governing permissions and | 
|  | 14 | +// limitations under the License. | 
|  | 15 | + | 
|  | 16 | +package rules | 
|  | 17 | + | 
|  | 18 | +import ( | 
|  | 19 | +	"context" | 
|  | 20 | +	"encoding/json" | 
|  | 21 | +	"fmt" | 
|  | 22 | +	"net/http" | 
|  | 23 | + | 
|  | 24 | +	"github.com/mia-platform/miactl/internal/client" | 
|  | 25 | +	"github.com/mia-platform/miactl/internal/resources" | 
|  | 26 | +	rulesentities "github.com/mia-platform/miactl/internal/resources/rules" | 
|  | 27 | +) | 
|  | 28 | + | 
|  | 29 | +const ( | 
|  | 30 | +	tenantsAPIPrefix     = "/api/backend/tenants/" | 
|  | 31 | +	projectsAPIPrefix    = "/api/backend/projects/" | 
|  | 32 | +	getProjectAPIFmt     = projectsAPIPrefix + "%s" | 
|  | 33 | +	patchTenantRulesFmt  = tenantsAPIPrefix + "%s/rules" | 
|  | 34 | +	patchProjectRulesFmt = projectsAPIPrefix + "%s/rules" | 
|  | 35 | +) | 
|  | 36 | + | 
|  | 37 | +type IRulesClient interface { | 
|  | 38 | +	ListTenantRules(ctx context.Context, companyID string) ([]*rulesentities.SaveChangesRules, error) | 
|  | 39 | +	ListProjectRules(ctx context.Context, projectID string) ([]*rulesentities.ProjectSaveChangesRules, error) | 
|  | 40 | +	UpdateTenantRules(ctx context.Context, companyID string, rules []*rulesentities.SaveChangesRules) error | 
|  | 41 | +	UpdateProjectRules(ctx context.Context, projectID string, rules []*rulesentities.SaveChangesRules) error | 
|  | 42 | +} | 
|  | 43 | + | 
|  | 44 | +type rulesClient struct { | 
|  | 45 | +	c *client.APIClient | 
|  | 46 | +} | 
|  | 47 | + | 
|  | 48 | +func New(c *client.APIClient) IRulesClient { | 
|  | 49 | +	return &rulesClient{c: c} | 
|  | 50 | +} | 
|  | 51 | + | 
|  | 52 | +func (e *rulesClient) ListTenantRules(ctx context.Context, companyID string) ([]*rulesentities.SaveChangesRules, error) { | 
|  | 53 | +	request := e.c.Get().APIPath(tenantsAPIPrefix) | 
|  | 54 | +	request.SetParam("search", companyID) | 
|  | 55 | + | 
|  | 56 | +	resp, err := request.Do(ctx) | 
|  | 57 | +	if err != nil { | 
|  | 58 | +		return nil, fmt.Errorf("error executing request: %w", err) | 
|  | 59 | +	} | 
|  | 60 | +	if err := e.assertSuccessResponse(resp); err != nil { | 
|  | 61 | +		return nil, err | 
|  | 62 | +	} | 
|  | 63 | + | 
|  | 64 | +	var tenants []resources.Company | 
|  | 65 | +	if err := resp.ParseResponse(&tenants); err != nil { | 
|  | 66 | +		return nil, fmt.Errorf("error parsing response body: %w", err) | 
|  | 67 | +	} | 
|  | 68 | +	if len(tenants) == 0 { | 
|  | 69 | +		return nil, fmt.Errorf("Company %s not found", companyID) | 
|  | 70 | +	} | 
|  | 71 | +	var tenant *resources.Company | 
|  | 72 | +	for _, possible := range tenants { | 
|  | 73 | +		if possible.TenantID == companyID { | 
|  | 74 | +			tenant = &possible | 
|  | 75 | +			break | 
|  | 76 | +		} | 
|  | 77 | +	} | 
|  | 78 | +	if tenant == nil { | 
|  | 79 | +		return nil, fmt.Errorf("Company %s not found", companyID) | 
|  | 80 | +	} | 
|  | 81 | +	if len(tenant.ConfigurationManagement.SaveChangesRules) == 0 { | 
|  | 82 | +		return []*rulesentities.SaveChangesRules{}, nil | 
|  | 83 | +	} | 
|  | 84 | + | 
|  | 85 | +	return tenant.ConfigurationManagement.SaveChangesRules, nil | 
|  | 86 | +} | 
|  | 87 | + | 
|  | 88 | +func (e *rulesClient) ListProjectRules(ctx context.Context, projectID string) ([]*rulesentities.ProjectSaveChangesRules, error) { | 
|  | 89 | +	request := e.c.Get().APIPath(fmt.Sprintf(getProjectAPIFmt, projectID)) | 
|  | 90 | +	request.SetParam("withTenant", "true") | 
|  | 91 | + | 
|  | 92 | +	resp, err := request.Do(ctx) | 
|  | 93 | +	if err != nil { | 
|  | 94 | +		return nil, fmt.Errorf("error executing request: %w", err) | 
|  | 95 | +	} | 
|  | 96 | +	if err := e.assertSuccessResponse(resp); err != nil { | 
|  | 97 | +		return nil, err | 
|  | 98 | +	} | 
|  | 99 | + | 
|  | 100 | +	var project resources.Project | 
|  | 101 | +	if err := resp.ParseResponse(&project); err != nil { | 
|  | 102 | +		return nil, fmt.Errorf("error parsing response body: %w", err) | 
|  | 103 | +	} | 
|  | 104 | + | 
|  | 105 | +	if len(project.ConfigurationManagement.SaveChangesRules) == 0 { | 
|  | 106 | +		return []*rulesentities.ProjectSaveChangesRules{}, nil | 
|  | 107 | +	} | 
|  | 108 | + | 
|  | 109 | +	return project.ConfigurationManagement.SaveChangesRules, nil | 
|  | 110 | +} | 
|  | 111 | + | 
|  | 112 | +type UpdateRequestBody struct { | 
|  | 113 | +	ConfigurationManagement *resources.ConfigurationManagement `json:"configurationManagement"` | 
|  | 114 | +} | 
|  | 115 | + | 
|  | 116 | +func (e *rulesClient) UpdateTenantRules(ctx context.Context, companyID string, rules []*rulesentities.SaveChangesRules) error { | 
|  | 117 | +	requestBody := UpdateRequestBody{ | 
|  | 118 | +		ConfigurationManagement: &resources.ConfigurationManagement{ | 
|  | 119 | +			SaveChangesRules: rules, | 
|  | 120 | +		}, | 
|  | 121 | +	} | 
|  | 122 | +	bodyData, err := json.Marshal(requestBody) | 
|  | 123 | +	if err != nil { | 
|  | 124 | +		return err | 
|  | 125 | +	} | 
|  | 126 | + | 
|  | 127 | +	request := e.c.Patch(). | 
|  | 128 | +		APIPath( | 
|  | 129 | +			fmt.Sprintf(patchTenantRulesFmt, companyID), | 
|  | 130 | +		). | 
|  | 131 | +		Body(bodyData) | 
|  | 132 | + | 
|  | 133 | +	resp, err := request.Do(ctx) | 
|  | 134 | +	if err != nil { | 
|  | 135 | +		return fmt.Errorf("error executing request: %w", err) | 
|  | 136 | +	} | 
|  | 137 | +	if err := e.assertSuccessResponse(resp); err != nil { | 
|  | 138 | +		return err | 
|  | 139 | +	} | 
|  | 140 | + | 
|  | 141 | +	return nil | 
|  | 142 | +} | 
|  | 143 | + | 
|  | 144 | +func (e *rulesClient) UpdateProjectRules(ctx context.Context, projectID string, rules []*rulesentities.SaveChangesRules) error { | 
|  | 145 | +	requestBody := UpdateRequestBody{ | 
|  | 146 | +		ConfigurationManagement: &resources.ConfigurationManagement{ | 
|  | 147 | +			SaveChangesRules: rules, | 
|  | 148 | +		}, | 
|  | 149 | +	} | 
|  | 150 | +	bodyData, err := json.Marshal(requestBody) | 
|  | 151 | +	if err != nil { | 
|  | 152 | +		return err | 
|  | 153 | +	} | 
|  | 154 | + | 
|  | 155 | +	request := e.c.Patch(). | 
|  | 156 | +		APIPath( | 
|  | 157 | +			fmt.Sprintf(patchProjectRulesFmt, projectID), | 
|  | 158 | +		). | 
|  | 159 | +		Body(bodyData) | 
|  | 160 | + | 
|  | 161 | +	resp, err := request.Do(ctx) | 
|  | 162 | +	if err != nil { | 
|  | 163 | +		return fmt.Errorf("error executing request: %w", err) | 
|  | 164 | +	} | 
|  | 165 | +	if err := e.assertSuccessResponse(resp); err != nil { | 
|  | 166 | +		return err | 
|  | 167 | +	} | 
|  | 168 | + | 
|  | 169 | +	return nil | 
|  | 170 | +} | 
|  | 171 | + | 
|  | 172 | +func (e *rulesClient) assertSuccessResponse(resp *client.Response) error { | 
|  | 173 | +	if resp.StatusCode() >= http.StatusBadRequest { | 
|  | 174 | +		return resp.Error() | 
|  | 175 | +	} | 
|  | 176 | +	return nil | 
|  | 177 | +} | 
0 commit comments