Skip to content

Commit

Permalink
Refactor import model (#1317)
Browse files Browse the repository at this point in the history
* break watcher logic away

At the end of ImportModel we perform a retrieval of deltas. Having this in it's own composed function reduces the cognitive complexity.

* add dial model

* update godoc

* move to jimm

* rename
  • Loading branch information
ale8k authored Aug 19, 2024
1 parent efad724 commit 58b8e61
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
41 changes: 19 additions & 22 deletions internal/jimm/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,25 +402,22 @@ func (j *JIMM) GetUserControllerAccess(ctx context.Context, user *openfga.User,
return ToControllerAccessString(accessLevel), nil
}

// ImportModel imports model with the specified uuid from the controller.
// ImportModel imports model with the specified UUID from the controller.
func (j *JIMM) ImportModel(ctx context.Context, user *openfga.User, controllerName string, modelTag names.ModelTag, newOwner string) error {
const op = errors.Op("jimm.ImportModel")

if !user.JimmAdmin {
return errors.E(op, errors.CodeUnauthorized, "unauthorized")
if err := j.checkJimmAdmin(user); err != nil {
return err
}

controller := dbmodel.Controller{
Name: controllerName,
}
err := j.Database.GetController(ctx, &controller)
controller, err := j.getControllerByName(ctx, controllerName)
if err != nil {
return errors.E(op, err)
}

api, err := j.dial(ctx, &controller, names.ModelTag{})
api, err := j.dialController(ctx, controller)
if err != nil {
return errors.E(op, err)
return errors.E(op, "failed to dial the controller", err)
}
defer api.Close()

Expand All @@ -438,7 +435,7 @@ func (j *JIMM) ImportModel(ctx context.Context, user *openfga.User, controllerNa
return errors.E(op, err)
}
model.ControllerID = controller.ID
model.Controller = controller
model.Controller = *controller

var ownerTag names.UserTag
if newOwner != "" {
Expand Down Expand Up @@ -513,19 +510,14 @@ func (j *JIMM) ImportModel(ctx context.Context, user *openfga.User, controllerNa
return errors.E(op, err)
}

regionFound := false
for _, cr := range cloud.Regions {
if cr.Name == modelInfo.CloudRegion {
regionFound = true
model.CloudRegion = cr
model.CloudRegionID = cr.ID
break
}
}
if !regionFound {
cr := cloud.Region(modelInfo.CloudRegion)
if cr.Name != modelInfo.CloudRegion {
return errors.E(op, "cloud region not found")
}

model.CloudRegionID = cr.ID
model.CloudRegion = cr

err = j.Database.AddModel(ctx, &model)
if err != nil {
if errors.ErrorCode(err) == errors.CodeAlreadyExists {
Expand All @@ -534,7 +526,13 @@ func (j *JIMM) ImportModel(ctx context.Context, user *openfga.User, controllerNa
return errors.E(op, err)
}

modelAPI, err := j.dial(ctx, &controller, modelTag)
return j.handleModelDeltas(ctx, controller, modelTag, model)
}

func (j *JIMM) handleModelDeltas(ctx context.Context, controller *dbmodel.Controller, modelTag names.ModelTag, model dbmodel.Model) error {
const op = errors.Op("jimm.getModelDeltas")

modelAPI, err := j.dialModel(ctx, controller, modelTag)
if err != nil {
return errors.E(op, err)
}
Expand Down Expand Up @@ -574,7 +572,6 @@ func (j *JIMM) ImportModel(ctx context.Context, user *openfga.User, controllerNa
return errors.E(op, err)
}
}

return nil
}

Expand Down
10 changes: 10 additions & 0 deletions internal/jimm/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,13 @@ func (j *JIMM) dialController(ctx context.Context, ctl *dbmodel.Controller) (API
}
return api, nil
}

// dialModel dials a model.
func (j *JIMM) dialModel(ctx context.Context, ctl *dbmodel.Controller, mt names.ModelTag) (API, error) {
api, err := j.dial(ctx, ctl, mt)
if err != nil {
zapctx.Error(ctx, "failed to dial the controller", zaputil.Error(err))
return nil, err
}
return api, nil
}

0 comments on commit 58b8e61

Please sign in to comment.