2021-08-26 14:00:59 +00:00
import KubernetesNamespaceHelper from 'Kubernetes/helpers/namespaceHelper' ;
2021-07-14 09:15:21 +00:00
export default class KubernetesRegistryAccessController {
/* @ngInject */
2021-09-06 05:25:02 +00:00
constructor ( $async , $state , ModalService , EndpointService , Notifications , KubernetesResourcePoolService ) {
2021-07-14 09:15:21 +00:00
this . $async = $async ;
this . $state = $state ;
2021-09-06 05:25:02 +00:00
this . ModalService = ModalService ;
2021-07-14 09:15:21 +00:00
this . Notifications = Notifications ;
this . KubernetesResourcePoolService = KubernetesResourcePoolService ;
this . EndpointService = EndpointService ;
this . state = {
actionInProgress : false ,
} ;
this . selectedResourcePools = [ ] ;
this . resourcePools = [ ] ;
this . savedResourcePools = [ ] ;
this . handleRemove = this . handleRemove . bind ( this ) ;
}
async submit ( ) {
return this . updateNamespaces ( [ ... this . savedResourcePools . map ( ( { value } ) => value ) , ... this . selectedResourcePools . map ( ( pool ) => pool . name ) ] ) ;
}
handleRemove ( namespaces ) {
const removeNamespaces = namespaces . map ( ( { value } ) => value ) ;
2021-09-06 05:25:02 +00:00
const nsToUpdate = this . savedResourcePools . map ( ( { value } ) => value ) . filter ( ( value ) => ! removeNamespaces . includes ( value ) ) ;
2021-07-14 09:15:21 +00:00
2021-09-06 05:25:02 +00:00
const displayedMessage =
'This registry might be used by one or more applications inside this environment. Removing the registry access could lead to a service interruption for these applications.<br/><br/>Do you wish to continue?' ;
this . ModalService . confirmDeletion ( displayedMessage , ( confirmed ) => {
if ( confirmed ) {
return this . updateNamespaces ( nsToUpdate ) ;
}
} ) ;
2021-07-14 09:15:21 +00:00
}
updateNamespaces ( namespaces ) {
return this . $async ( async ( ) => {
try {
await this . EndpointService . updateRegistryAccess ( this . endpoint . Id , this . registry . Id , {
namespaces ,
} ) ;
2021-09-24 08:21:50 +00:00
this . $state . reload ( this . $state . current ) ;
2021-07-14 09:15:21 +00:00
} catch ( err ) {
this . Notifications . error ( 'Failure' , err , 'Failed saving registry access' ) ;
}
} ) ;
}
$onInit ( ) {
return this . $async ( async ( ) => {
try {
this . state = {
registryId : this . $state . params . id ,
} ;
this . registry = await this . EndpointService . registry ( this . endpoint . Id , this . state . registryId ) ;
if ( this . registry . RegistryAccesses && this . registry . RegistryAccesses [ this . endpoint . Id ] ) {
this . savedResourcePools = this . registry . RegistryAccesses [ this . endpoint . Id ] . Namespaces . map ( ( value ) => ( { value } ) ) ;
}
} catch ( err ) {
this . Notifications . error ( 'Failure' , err , 'Unable to retrieve registry details' ) ;
}
try {
const resourcePools = await this . KubernetesResourcePoolService . get ( ) ;
this . resourcePools = resourcePools
2021-08-26 14:00:59 +00:00
. filter ( ( pool ) => ! KubernetesNamespaceHelper . isSystemNamespace ( pool . Namespace . Name ) && ! this . savedResourcePools . find ( ( { value } ) => value === pool . Namespace . Name ) )
2021-07-14 09:15:21 +00:00
. map ( ( pool ) => ( { name : pool . Namespace . Name , id : pool . Namespace . Id } ) ) ;
} catch ( err ) {
this . Notifications . error ( 'Failure' , err , 'Unable to retrieve namespaces' ) ;
}
} ) ;
}
}