2주차 과제 - 이성현

필수과제
과제 이해를 잘못해가지고 python-openstackclient에서 server list의 코드를 수정방식으로 다시 진행한 내용

# Add project name and user name attributes
        for s in data:
            # Get project name - try different attribute names
            project_id = None
            if hasattr(s, 'project_id') and s.project_id:
                project_id = s.project_id
                LOG.debug('Found project_id: %s', project_id)
            elif hasattr(s, 'tenant_id') and s.tenant_id:
                project_id = s.tenant_id
                LOG.debug('Found tenant_id: %s', project_id)
            elif hasattr(s, 'OS-EXT-AZ:project_id') and getattr(s, 'OS-EXT-AZ:project_id'):
                project_id = getattr(s, 'OS-EXT-AZ:project_id')
                LOG.debug('Found OS-EXT-AZ:project_id: %s', project_id)
            
            # If project_id not found in server object, try to get it from server details
            if not project_id:
                try:
                    server_details = compute_client.get_server(s.id)
                    if hasattr(server_details, 'project_id') and server_details.project_id:
                        project_id = server_details.project_id
                        LOG.debug('Found project_id from server details: %s', project_id)
                    elif hasattr(server_details, 'tenant_id') and server_details.tenant_id:
                        project_id = server_details.tenant_id
                        LOG.debug('Found tenant_id from server details: %s', project_id)
                except Exception as e:
                    LOG.debug('Failed to get server details for %s: %s', s.id, e)
            
            if project_id:
                try:
                    project = identity_common.find_project(
                        identity_client,
                        project_id,
                        None
                    )
                    s.project_name = project.name
                    LOG.debug('Successfully got project name: %s', project.name)
                except Exception as e:
                    LOG.debug('Failed to get project name for ID %s: %s', project_id, e)
                    s.project_name = 'N/A'
            else:
                LOG.debug('No project_id found for server %s', s.id)
                # Try to get project name from current session
                try:
                    current_project = identity_client.get_current_project()
                    s.project_name = current_project.name
                    LOG.debug('Using current project name: %s', current_project.name)
                except Exception as e:
                    LOG.debug('Failed to get current project: %s', e)
                    s.project_name = 'N/A'

            # Get user name - try different attribute names
            user_id = None
            if hasattr(s, 'user_id') and s.user_id:
                user_id = s.user_id
                LOG.debug('Found user_id: %s', user_id)
            elif hasattr(s, 'OS-EXT-AZ:user_id') and getattr(s, 'OS-EXT-AZ:user_id'):
                user_id = getattr(s, 'OS-EXT-AZ:user_id')
                LOG.debug('Found OS-EXT-AZ:user_id: %s', user_id)
            
            # If user_id not found in server object, try to get it from server details
            if not user_id:
                try:
                    server_details = compute_client.get_server(s.id)
                    if hasattr(server_details, 'user_id') and server_details.user_id:
                        user_id = server_details.user_id
                        LOG.debug('Found user_id from server details: %s', user_id)
                except Exception as e:
                    LOG.debug('Failed to get server details for %s: %s', s.id, e)
            
            if user_id:
                try:
                    user = identity_common.find_user(
                        identity_client,
                        user_id,
                        None
                    )
                    s.user_name = user.name
                    LOG.debug('Successfully got user name: %s', user.name)
                except Exception as e:
                    LOG.debug('Failed to get user name for ID %s: %s', user_id, e)
                    s.user_name = 'N/A'
            else:
                LOG.debug('No user_id found for server %s', s.id)
                # Try to get user name from current session
                try:
                    current_user = identity_client.get_current_user()
                    s.user_name = current_user.name
                    LOG.debug('Using current user name: %s', current_user.name)
                except Exception as e:
                    LOG.debug('Failed to get current user: %s', e)
                    s.user_name = 'N/A'

선택과제

  1. 명령 입력: openstack server list 명령이 입력되면 cliff 프레임워크를 통해 파싱
  2. 클래스 호출: openstackclient/compute/v2/server.pyListServer 클래스 take_action() 메소드 실행
  3. 데이터 조회: compute_client.servers.list()로 서버 목록을 가져오고, _prep_server_detail()로 추가 정보 추가 한 후에 utils.get_item_properties()로 컬럼별 데이터 추출
  4. 결과 반환: (column_headers, data) 튜플 형태로 반환하면 출력부(emit_list)에서 최종 테이블 생성하여 화면에 출력