public async Task <IActionResult> Edit(long?id, [FromBody] EditInputModel model)
        {
            //データの入力チェック
            if (!ModelState.IsValid || !id.HasValue)
            {
                return(JsonBadRequest("Invalid inputs."));
            }
            //データの存在チェック
            var tenant = await tenantRepository.GetTenantWithStorageForUpdateAsync(id.Value);

            if (tenant == null)
            {
                return(JsonNotFound($"Tenant ID {id.Value} is not found."));
            }

            if (model.DefaultGitId != null && model.GitIds.Contains(model.DefaultGitId.Value) == false)
            {
                //デフォルトGitがGit一覧の中になかったらエラー
                return(JsonConflict($"Default Git ID {model.DefaultGitId.Value} does NOT exist in selected gits."));
            }
            if (model.DefaultRegistryId != null && model.RegistryIds.Contains(model.DefaultRegistryId.Value) == false)
            {
                //デフォルトレジストリがレジストリ一覧の中になかったらエラー
                return(JsonConflict($"Default Registry ID {model.DefaultRegistryId.Value} does NOT exist in selected registries."));
            }
            if (model.StorageId != null)
            {
                //データの存在チェック
                var storage = tenantRepository.GetStorage(model.StorageId.Value);
                if (storage == null)
                {
                    return(JsonNotFound($"The selected storage ID {model.StorageId.Value} is not found."));
                }

                //バケットを作成する
                await storageLogic.CreateBucketAsync(tenant, storage);
            }

            tenant.DisplayName = model.DisplayName;
            tenant.StorageId   = model.StorageId;

            //コンテナ管理サービス作業
            //テナントを登録
            var tenantResult = await clusterManagementLogic.RegistTenantAsync(tenant.Name);

            if (tenantResult == false)
            {
                return(JsonError(HttpStatusCode.ServiceUnavailable, "Couldn't create cluster master namespace. Please check the configuration to the connect cluster manager service."));
            }

            //テナントとGitを紐づけ
            //まずは現状のGitを取得して、そこから増減を判断する
            var currentGits = gitRepository.GetGitAll(tenant.Id).ToList();

            if (model.GitIds != null && model.GitIds.Count() > 0)
            {
                //デフォルトGitの設定(無ければ一個目)
                tenant.DefaultGitId = model.DefaultGitId == null?
                                      model.GitIds.ElementAt(0) : model.DefaultGitId.Value;

                foreach (long gitId in model.GitIds)
                {
                    Git currentGit = currentGits.FirstOrDefault(r => r.Id == gitId);
                    if (currentGit != null)
                    {
                        //以前も紐づいていたので、無視。
                        currentGits.Remove(currentGit);
                        continue;
                    }

                    //データの存在チェック
                    Git git = await gitRepository.GetByIdAsync(gitId);

                    if (git == null)
                    {
                        return(JsonNotFound($"The selected git ID {gitId} is not found."));
                    }

                    await gitRepository.AttachGitToTenantAsync(tenant, git, false);
                }
            }
            //残っているのは削除された紐づけなので、消す
            foreach (var removedGit in currentGits)
            {
                gitRepository.DetachGitFromTenant(tenant, removedGit);
            }

            //テナントとレジストリを紐づけ
            //まずは現状のレジストリを取得して、そこから増減を判断する
            var currentRegistries = registryRepository.GetRegistryAll(tenant.Id).ToList();

            if (model.RegistryIds != null && model.RegistryIds.Count() > 0)
            {
                //デフォルトレジストリの設定(無ければ一個目)
                tenant.DefaultRegistryId = model.DefaultRegistryId == null?
                                           model.RegistryIds.ElementAt(0) : model.DefaultRegistryId.Value;

                foreach (long registryId in model.RegistryIds)
                {
                    Registry currentRegistry = currentRegistries.FirstOrDefault(r => r.Id == registryId);
                    if (currentRegistry != null)
                    {
                        //以前も紐づいていたので、無視。
                        currentRegistries.Remove(currentRegistry);
                        continue;
                    }

                    //データの存在チェック
                    Registry registry = await registryRepository.GetByIdAsync(registryId);

                    if (registry == null)
                    {
                        return(JsonNotFound($"The selected registry ID {registryId} is not found."));
                    }

                    var maps = await registryRepository.AttachRegistryToTenantAsync(tenant, registry, false);

                    if (maps != null)
                    {
                        foreach (var map in maps)
                        {
                            //レジストリを登録
                            var registryResult = await clusterManagementLogic.RegistRegistryToTenantAsync(tenant.Name, map);

                            if (registryResult == false)
                            {
                                return(JsonError(HttpStatusCode.ServiceUnavailable, "Couldn't map the tenant and the registry in a cluster management service. Please check the configuration to the connect cluster manager service."));
                            }
                        }
                    }
                }
            }
            //残っているのは削除された紐づけなので、消す
            foreach (var removedRegistry in currentRegistries)
            {
                registryRepository.DetachRegistryFromTenant(tenant, removedRegistry);
            }

            // 関連するクラスタトークンをリセット
            tenantRepository.DeleteClusterToken(tenant.Id);

            tenantRepository.Update(tenant, unitOfWork);

            return(JsonOK(new IndexOutputModel(tenant)));
        }